Skip to content

Instantly share code, notes, and snippets.

@cagdas1
Last active February 16, 2020 18:41
Show Gist options
  • Save cagdas1/3c1449df10d9cd7131e0473308fae66c to your computer and use it in GitHub Desktop.
Save cagdas1/3c1449df10d9cd7131e0473308fae66c to your computer and use it in GitHub Desktop.
import * as ec2 from "@aws-cdk/aws-ec2";
import * as cdk from '@aws-cdk/core';
export class MyEc2AppStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create 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 });
// Instance details
const ec2Instance = new ec2.Instance(this, 'Instance', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
machineImage: awsAMI,
securityGroup: mySecurityGroup
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment