|
| 1 | +--- |
| 2 | +title: "Create a sample AWS CDK application" |
| 3 | +description: Create a JavaScript AWS CDK application that defines an Amazon ECS service running on Arm-based AWS Fargate compute. |
| 4 | +weight: 2 |
| 5 | + |
| 6 | +layout: "learningpathall" |
| 7 | +--- |
| 8 | + |
| 9 | +## Set up a sample AWS CDK application |
| 10 | + |
| 11 | +The AWS Cloud Development Kit (CDK) is an open-source infrastructure as code (IaC) software development framework. |
| 12 | + |
| 13 | +In this section, you'll create a JavaScript CDK application that defines an Amazon Elastic Container Service (ECS) service running on Arm-based AWS Fargate compute. |
| 14 | + |
| 15 | +Arm-based AWS compute is powered by AWS Graviton processors. For more information about AWS Graviton, see [Level up your compute with AWS Graviton](https://aws.amazon.com/ec2/graviton/level-up-with-graviton/). |
| 16 | + |
| 17 | +### Before you begin |
| 18 | + |
| 19 | +Make sure that you've installed the AWS CDK CLI: |
| 20 | + |
| 21 | +```bash |
| 22 | +cdk --version |
| 23 | +``` |
| 24 | + |
| 25 | +The output is similar to: |
| 26 | + |
| 27 | +```output |
| 28 | +2.1125.0 (build 71fd29e) |
| 29 | +``` |
| 30 | + |
| 31 | +For instructions to set up and install the AWS CDK CLI, see the [AWS CDK install guide](/install-guides/aws-cdk). |
| 32 | + |
| 33 | +Make sure you've installed Node.js: |
| 34 | + |
| 35 | +```bash |
| 36 | +node --version |
| 37 | +``` |
| 38 | + |
| 39 | +The output is similar to: |
| 40 | +```output |
| 41 | +v26.2.0 |
| 42 | +``` |
| 43 | + |
| 44 | +### Initialize a CDK project |
| 45 | + |
| 46 | +Create a directory for your CDK project and navigate to it: |
| 47 | + |
| 48 | +```bash |
| 49 | +mkdir arm-cdk-app |
| 50 | +cd arm-cdk-app/ |
| 51 | +``` |
| 52 | + |
| 53 | +After navigating into the project directory, initialize a JavaScript CDK project: |
| 54 | + |
| 55 | +```bash |
| 56 | +cdk init --language javascript |
| 57 | +``` |
| 58 | + |
| 59 | +The output is similar to: |
| 60 | + |
| 61 | +```output |
| 62 | +Applying project template app for javascript |
| 63 | +# Welcome to your CDK JavaScript project |
| 64 | +
|
| 65 | +This is a blank project for CDK development with JavaScript. |
| 66 | +
|
| 67 | +The `cdk.json` file tells the CDK Toolkit how to execute your app. The build step is not required when using JavaScript. |
| 68 | +
|
| 69 | +## Useful commands |
| 70 | +
|
| 71 | +* `npm run test` perform the jest unit tests |
| 72 | +* `npx cdk deploy` deploy this stack to your default AWS account/region |
| 73 | +* `npx cdk diff` compare deployed stack with current state |
| 74 | +* `npx cdk synth` emits the synthesized CloudFormation template |
| 75 | +
|
| 76 | +... |
| 77 | +``` |
| 78 | + |
| 79 | +### Use the AWS CDK with JavaScript to define a sample application |
| 80 | + |
| 81 | +In the project, you'll find a file called `arm-cdk-app-stack.js` in the `lib` directory. AWS CDK uses this stack definition to deploy all necessary AWS resources. |
| 82 | + |
| 83 | +Update `lib/arm-cdk-app-stack.js` to define a load-balanced Amazon ECS service that runs an NGINX web server on an Arm-based AWS Fargate runtime platform: |
| 84 | + |
| 85 | +```javascript |
| 86 | +const cdk = require('aws-cdk-lib'); |
| 87 | +const ecs = require('aws-cdk-lib/aws-ecs'); |
| 88 | +const ecsPatterns = require('aws-cdk-lib/aws-ecs-patterns'); |
| 89 | + |
| 90 | +class ArmCdkAppStack extends cdk.Stack { |
| 91 | + constructor(scope, id, props) { |
| 92 | + super(scope, id, props); |
| 93 | + |
| 94 | + new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Service', { |
| 95 | + taskImageOptions: { |
| 96 | + image: ecs.ContainerImage.fromRegistry("nginx:latest"), |
| 97 | + containerPort: 80, |
| 98 | + }, |
| 99 | + runtimePlatform: { |
| 100 | + cpuArchitecture: ecs.CpuArchitecture.ARM64, |
| 101 | + operatingSystemFamily: ecs.OperatingSystemFamily.LINUX, |
| 102 | + }, |
| 103 | + publicLoadBalancer: true, |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { ArmCdkAppStack }; |
| 109 | +``` |
| 110 | + |
| 111 | +## What you've accomplished and what's next |
| 112 | + |
| 113 | +You've now set up a sample application using AWS CDK. |
| 114 | + |
| 115 | +Next, you'll use AWS CDK to synthesize and deploy the application. |
0 commit comments