Skip to content

Instantly share code, notes, and snippets.

@sohelamin
Created June 24, 2020 15:26
Show Gist options
  • Save sohelamin/0e8bf43337ff55ee1a4b5d5069aa3128 to your computer and use it in GitHub Desktop.
Save sohelamin/0e8bf43337ff55ee1a4b5d5069aa3128 to your computer and use it in GitHub Desktop.
AWS CloudFormation EC2
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone: us-east-1a
ImageId: ami-068663a3c619dd892 # Ubuntu Server 20.04 LTS
InstanceType: t2.micro
KeyName: my-test-key # Existing key pair
SecurityGroups:
- !Ref MyEC2SecurityGroup
Tags:
-
Key: Name
Value: My EC2 Instance
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update
apt-get -y install nginx
service nginx start
MyEIP:
Type: AWS::EC2::EIP
Properties:
InstanceId: !Ref MyEC2Instance
MyEC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: My EC2 Security group
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
- CidrIp: 0.0.0.0/0
FromPort: 443
IpProtocol: tcp
ToPort: 443
@sohelamin
Copy link
Author

sohelamin commented Jun 24, 2020

Usage

aws ec2 create-key-pair --key-name my-test-key
aws cloudformation create-stack --stack-name my-test-stack --template-body file://cloudformation-ec2.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment