Deploy EC2 instance using the AWS CDK for Python
Make sure you do this setup first:
-
For Python support, install
pyenv
:brew install pyenv
Configure project:
source configure.sh
Open in Visual Studio Code:
code .
Print the CloudFormation template for the stack. You should see the CloudFormation template without any errors:
cdk synth --profile default
Bootstrap the CDK environment. This should be done only once per account. Skip this step if you have done it already. See Bootstrapping for details:
cdk bootstrap --profile default
Deploy the stack:
cdk deploy --profile default
Check that you can browse the nginx
default site:
key="aws-ec2-key"
instance="NginxEc2Stack/NginxInstance"
instance_public_ip=$(aws ec2 describe-instances \
--filters \
Name=tag:Name,Values=$instance \
Name=instance-state-name,Values=running \
| jq -r '.Reservations[0].Instances[0].PublicIpAddress')
open http://$instance_public_ip
cdk destroy --profile default
Opptional: To delete the CDKToolkit
CloudFormation template which is created by the AWS CDK during bootstrap:
aws cloudformation delete-stack --stack-name CDKToolkit --profile default
The bootstrap process creates an S3 Bucket with a name: cdk-<hash>-assets-<account_id>-<region>
. This bucket will not be deleted automatically and will remain in your account if not deleted manually.
# Node.js
nvm use 20.16.0
# Python
mkdir ec2-aws-cdk-python
cd ec2-aws-cdk-python
cdk init app --language python --generate-only
mv README.md README_MDK.md