4. Deploy the AWS Backend

Next, we will synthesize and output to the terminal the CloudFormation template that gets generated by the AWS CDK to build your backend in AWS.

We need to decide on an environment name that will be used to configure and deploy your environment. The value chosen for your environment name is used to create S3 buckets for the application, so we will need to select a name that will avoid S3 bucket name collision (keep in mind that S3 bucket names are global).

aws cdk synthesize

  1. Visit the terminal that is not currently running the npm run watch command. Your prompt should still look like this ~/environment/aws-alien-attack/infrastructure/cdk (master) $
  2. Use the cdk synthesize command for CDK to synthesize your environment.
cdk synthesize -c envname=$envname

If the synthesize command is successful, it will generate an output for the corresponding CloudFormation template. This template will show all the AWS resources that will be created when you deploy your Alien Attack environment. You can save it by redirecting the result to some folder, so you can read it through.

For example, you could redirect it to a file named output.yaml with the following command:

cdk synthesize -c envname=$envname > ../output.yaml

This creates a file named output.yaml in your folder ~/environment/aws-alien-attack/infrastructure. Open it and explore its content.

If the command was not successful, this means you had an S3 bucket name collision. If the message in your terminal looks similar to the one below, please choose another environment name for your deployment and run through the above steps once more.

An unsuccessful synthesize command will look something like the output below:

Environment name: TEST
[ { Forbidden: null
      at Request.extractError (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/services/s3.js:565:35)
      at Request.callListeners (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
      at Request.emit (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/request.js:683:14)
      at Request.transition (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/state_machine.js:14:12)
      at /home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/state_machine.js:26:10
      at Request.<anonymous> (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/request.js:38:9)
      at Request.<anonymous> (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/request.js:685:12)
      at Request.callListeners (/home/ec2-user/environment/alienattack.workshop/cdk/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    message: null,
    code: 'Forbidden',
    region: 'ap-southeast-1',
    time: 2018-12-21T18:55:32.875Z,
    requestId: '99ECF0537A7B2AD1',
    cfId: undefined,
    statusCode: 403,
    retryable: false,
    retryDelay: 133.93524816013246 } ]
Unable to find output file /tmp/cdkF6Q2pM/cdk.out; are you calling app.run()?

aws cdk deploy

Once you have successfully synthesized your environment, run the following command to deploy that environment:

cdk deploy -c envname=$envname

This will deploy the previously seen CloudFormation stack inside of your AWS account. In turn, this CloudFormation stack will spin up the AWS resources in your Alien Attack backend.

The AWS CDK will first show you what changes will be applied to the environment. After that, it will ask you to confirm whether or not you want to deploy these resources. Enter y, and wait for your environment to be deployed. This may take a few minutes.

If deployment was successful, you should see something like this in your terminal:

 ✅  YourEnvironmentName

<YourEnvironmentName>.apigtw = https://<api-id>.execute-api.<region>.amazonaws.com/prod/v1/
<YourEnvironmentName>.envname = <YourEnvironmentName>
<YourEnvironmentName>.region = <region>

Stack ARN:

You should see a section called Outputs. Copy those outputs and paste them into a text editor. You will need these outputs for later in the workshop.

The synthesize command will have also created two S3 buckets named YOUR_ENVIRONMENT_NAME.app and YOUR_ENVIRONMENT_NAME.raw. Visit the Amazon S3 Console to see.

You can also visit the AWS CloudFormation Console in a separate window to monitor your environment’s progress.