Deploy a Web Application on Amazon EC2

GETTING STARTED GUIDE

Module 3: Deploy CDK Stack

In this module, you will deploy your infrastructure and application.

Introduction

With the AWS CDK app created, resources defined, and a user data asset for installing software on first launch, you are ready to deploy your stack and create the resources. In this module, you will deploy the stack using CDK.

What You Will Learn

  • Bootstrap CDK
  • Deploy all the infrastructure using CDK

 Time to Complete

5 minutes

 Module Prereqs

  • AWS Account with administrator-level access**
  • Recommended browser: The latest version of Chrome or Firefox

[**]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

Implementation

Bootstrap CDK

Before you can deploy your CDK app, you need to set up CDK on the account you are deploying to. Edit the bin/cdk-ecs-infra.ts file, and uncomment line 14:

env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

This will use the Account ID and region configured in the AWS CLI. Before you can use CDK, it needs to be bootstrapped - this will create the required infrastructure for CDK to manage infrastructure in your account. To bootstrap CDK, run cdk bootstrap

You should see output similar to:

cdk bootstrap

#output
⏳  Bootstrapping environment aws://0123456789012/<region>...
✅  Environment aws://0123456789012/<region> bootstrapped

Deploying the stack

Once the bootstrapping has completed, you will run cdk deploy to deploy all the infrastructure you created. You will be presented with the following output, and confirmation screen.

As our stack has security implications, you'll see a summary of these and need to confirm them before deployment proceeds.

gsg-build-ec2-1

Enter y to continue with the deployment and create the resources. The CLI will show the deployment progress and in the end the output we defined in our CDK app.

Do you wish to deploy these changes (y/n)? y
Ec2CdkStack: deploying...
[0%] start: Publishing afe67465ec62603d27d77795221a45e68423c87495467b0265ecdadad80bb5e2:current
[33%] success: Published afe67465ec62603d27d77795221a45e68423c87495467b0265ecdadad80bb5e2:current
[33%] start: Publishing 73887b77b71ab7247eaf6dc4647f03f9f1cf8f0da685460f489ec8f2106d480d:current
[66%] success: Published 73887b77b71ab7247eaf6dc4647f03f9f1cf8f0da685460f489ec8f2106d480d:current
[66%] start: Publishing 13138ebf2da51426144f6f5f4f0ad197787f52aad8b6ceb26ecff68d33cd2b78:current
[100%] success: Published 13138ebf2da51426144f6f5f4f0ad197787f52aad8b6ceb26ecff68d33cd2b78:current
Ec2CdkStack: creating CloudFormation changeset...

✅  Ec2CdkStack

Outputs:
Ec2CdkStack.DownloadKeyCommand = aws secretsmanager get-secret-value --secret-id ec2-ssh-key/cdk-keypair/private --query SecretString --output text > cdk-key.pem && chmod 400 cdk-key.pem
Ec2CdkStack.IPAddress = 54.75.32.202
Ec2CdkStack.sshcommand = ssh -i cdk-key.pem -o IdentitiesOnly=yes ec2-user@54.75.32.202

Stack ARN:
arn:aws:cloudformation:eu-west-1:123456789012:stack/Ec2CdkStack/c8bde0b0-16ed-11ec-a147-0a4fed479a1b

Your application is now deployed, and you can use the outputs at the bottom to download the SSH key, and then access the EC2 instance if you need to. You can also access the application in your browser by pasting in the IP printed above.

Conclusion

In this module, you learned how to bootstrap CDK to your account, and use it to deploy your infrastructure and web application. In the next module, you will clean up all the resources created in this guide.

Up Next: Clean-up Resources

Let us know how we did.

Thank you for your feedback
We're glad this page helped you. Would you like to share additional details to help us continue to improve?
Close
Thank you for your feedback
We're sorry this page didn't help you. Would you like to share additional details to help us continue to improve?
Close