Skip to content

Instantly share code, notes, and snippets.

@cagdas1
Created February 16, 2023 14:03
Show Gist options
  • Save cagdas1/c385f5b2dc923ac5aad4d523cbd784ba to your computer and use it in GitHub Desktop.
Save cagdas1/c385f5b2dc923ac5aad4d523cbd784ba to your computer and use it in GitHub Desktop.
CDK Migration Post
import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import { config } from "dotenv";
config();
class EC2BasicsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a new VPC
const vpc = new ec2.Vpc(this, 'VPC');
// Open port 22 for SSH connection from anywhere
const mySecurityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc,
securityGroupName: "my-test-sg",
description: 'Allow ssh access to ec2 instances from anywhere',
allowAllOutbound: true
});
mySecurityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22), 'allow public ssh access')
// We are using the latest AMAZON LINUX AMI
const awsAMI = new ec2.AmazonLinuxImage({generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2});
// We define the instance details here
const ec2Instance = new ec2.Instance(this, 'Instance', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
machineImage: awsAMI,
securityGroup: mySecurityGroup
});
}
}
const app = new cdk.App();
new EC2BasicsStack(app, "EC2BasicsStack", {
env: {
region: process.env.AWS_REGION,
account: process.env.AWS_ACCOUNT_ID
}
});
import * as cdk from 'aws-cdk-lib';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { config } from "dotenv";
import { Construct } from 'constructs';
config();
class EC2BasicsStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a new VPC
const vpc = new ec2.Vpc(this, 'VPC');
// Open port 22 for SSH connection from anywhere
const mySecurityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc,
securityGroupName: "my-test-sg",
description: 'Allow ssh access to ec2 instances from anywhere',
allowAllOutbound: true
});
mySecurityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22), 'allow public ssh access')
// We are using the latest AMAZON LINUX AMI
const awsAMI = new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 });
// We define the instance details here
const ec2Instance = new ec2.Instance(this, 'Instance', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
machineImage: awsAMI,
securityGroup: mySecurityGroup,
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC
},
});
new cdk.CfnOutput(this, "ip-address", {value: ec2Instance.instancePublicIp});
}
}
const app = new cdk.App();
new EC2BasicsStack(app, "EC2BasicsStack", {
env: {
region: process.env.AWS_REGION,
account: process.env.AWS_ACCOUNT_ID
}
});
{
"name": "ec2-basics",
"version": "0.1.0",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"@types/node": "8.10.45",
"aws-cdk": "^1.25.0",
"ts-node": "^8.6.2",
"typescript": "^3.8.2"
},
"dependencies": {
"@aws-cdk/aws-ec2": "^1.25.0",
"@aws-cdk/core": "^1.25.0",
"dotenv": "^8.2.0",
"source-map-support": "^0.5.9"
}
}
{
"name": "ec2-basics",
"version": "0.1.0",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"aws-cdk": "2.53.0",
"@types/babel__traverse": "7.18.2",
"@types/jest": "^27.5.2",
"@types/node": "10.17.27",
"@types/prettier": "2.6.0",
"jest": "^27.5.1",
"ts-jest": "^27.1.4",
"ts-node": "^10.9.1",
"typescript": "~3.9.7"
},
"dependencies": {
"aws-cdk-lib": "2.53.0",
"constructs": "^10.0.0",
"dotenv": "^8.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment