AWS CDK EC2 Construct Library (v1)

1.204.0 · maintenance · verified Thu Apr 16

This is the AWS Cloud Development Kit (CDK) Construct Library for AWS EC2, part of the AWS CDK v1 ecosystem. It provides high-level object-oriented abstractions to define EC2 resources and networking in Python, which are then provisioned via AWS CloudFormation. AWS CDK v1 is currently in maintenance mode, with active development focused on AWS CDK v2. The AWS CDK (including this library) generally follows a continuous release cadence for minor versions within its major release lines.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code defines an AWS CDK v1 stack in Python that provisions a new VPC with public and private subnets, and an EC2 T3.Micro instance running Amazon Linux 2 within that VPC. It then outputs the public IP address of the instance. Replace 'MyKeyPair' with an existing EC2 Key Pair in your AWS account. You'll need to run `cdk bootstrap` and configure `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` environment variables before deploying with `cdk deploy`.

import os
from aws_cdk import (
    core,
    aws_ec2 as ec2
)

class Ec2Stack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create a VPC
        vpc = ec2.Vpc(self, "MyVpc",
                      cidr="10.0.0.0/16",
                      max_azs=2,
                      subnet_configuration=[
                          ec2.SubnetConfiguration(
                              name="Public",
                              subnet_type=ec2.SubnetType.PUBLIC,
                              cidr_mask=24
                          ),
                          ec2.SubnetConfiguration(
                              name="Private",
                              subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS,
                              cidr_mask=24
                          )
                      ]
                     )

        # Define an Amazon Linux 2 AMI
        ami = ec2.MachineImage.latest_amazon_linux(
            generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
            edition=ec2.AmazonLinuxEdition.STANDARD,
            virtualization=ec2.AmazonLinuxVirt.HVM,
            storage=ec2.AmazonLinuxStorage.GENERAL_PURPOSE
        )

        # Create an EC2 instance
        instance = ec2.Instance(self, "MyInstance",
                                vpc=vpc,
                                instance_type=ec2.InstanceType.of(
                                    ec2.InstanceClass.T3,
                                    ec2.InstanceSize.MICRO
                                ),
                                machine_image=ami,
                                key_name="MyKeyPair" # Ensure this key pair exists in your AWS account
                               )

        # Output the public IP address of the EC2 instance
        core.CfnOutput(self, "InstancePublicIp", value=instance.instance_public_ip)

app = core.App()
Ec2Stack(app, "MyEc2Stack",
         env=core.Environment(
             account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
             region=os.environ.get("CDK_DEFAULT_REGION")
         )
)
app.synth()

view raw JSON →