Skip to content

Instantly share code, notes, and snippets.

@caike
Last active April 27, 2020 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/7310b74fa2ebbcd7bffc2125d252f4b0 to your computer and use it in GitHub Desktop.
Save caike/7310b74fa2ebbcd7bffc2125d252f4b0 to your computer and use it in GitHub Desktop.
CloudFormation templates. Must use Linux 2 AMI images because of systemd.
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Creates an EC2 instance with Nginx installed and running.'
Parameters:
VPC:
Description: VPC for the SecurityGroup
Type: AWS::EC2::VPC::Id
Subnet:
Description: Subnet for the EC2 Instance
Type: AWS::EC2::Subnet::Id
ImageId:
Description: Image ID for the AMI to be used
Type: AWS::EC2::Image::Id
# Region dependent. For US-EAST-1, using Linux 2 AMI because systemd ami-0fc61db8544a617ed
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
files:
/var/www/index.html:
content: !Sub |
<html><head><title>Simple Static Stack</title></head><body><h1>Hello from Pluralsight Hands-On Labs!<h1></body></html>
/etc/nginx/nginx.conf:
content: !Sub |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log /var/log/nginx/access.log combined;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
root /var/www;
index index.html;
}
}
commands:
01_set_permissions:
command: chown -R nginx:nginx /var/www
02_enable_nginx:
command: systemctl enable nginx
03_start_nginx:
command: systemctl start nginx
Properties:
InstanceType: t2.nano
ImageId: !Ref ImageId
NetworkInterfaces:
- AssociatePublicIpAddress: 'True'
DeleteOnTermination: 'True'
SubnetId: !Ref Subnet
DeviceIndex: '0'
GroupSet: [!GetAtt InstanceSecurityGroup.GroupId]
UserData:
'Fn::Base64':
!Sub |
#!/bin/bash -xe
yum update -y
yum install -y aws-cfn-bootstrap
amazon-linux-extras install nginx1.12 -y
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
Tags:
- Key: Name
Value: EC2FromCloudFormationCLI
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
Outputs:
InstanceId:
Description: InstanceId of the newly created EC2 instance
Value:
Ref: EC2Instance
AZ:
Description: Availability Zone of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- AvailabilityZone
PublicDNS:
Description: Public DNSName of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicDnsName
PublicIP:
Description: Public IP address of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicIp
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Create a Node HTTP API with nginx as a reverse proxy'
Parameters:
VPC:
Description: VPC for the SecurityGroup
Type: AWS::EC2::VPC::Id
Subnet:
Description: Subnet for the EC2 Instance
Type: AWS::EC2::Subnet::Id
ImageId:
Description: Image ID for the AMI to be used
Type: AWS::EC2::Image::Id
# Region dependent. For US-EAST-1, using Linux 2 AMI because systemd ami-0fc61db8544a617ed
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
files:
/etc/nginx/nginx.conf:
content: !Sub |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log /var/log/nginx/access.log combined;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
}
}
}
/etc/systemd/system/node-app.service:
content: !Sub |
[Unit]
Description=Node API
After=network.target
[Service]
User=ec2-user
Environment=NODE_ENV=production
Environment=PORT=3000
WorkingDirectory=/home/ec2-user/meetup-api
ExecStart=/usr/bin/npm start
Restart=always
RestartSec=500ms
StartLimitInterval=0
[Install]
WantedBy=multi-user.target
commands:
01_git_clone:
command: 'git clone https://github.com/OrlandoJS/meetup-api.git'
cwd: '/home/ec2-user/'
02_npm_install:
command: 'npm install'
cwd: '/home/ec2-user/meetup-api'
03_enable_app:
command: systemctl enable node-app
04_start_app:
command: systemctl start node-app
05_enable_nginx:
command: systemctl enable nginx
06_start_nginx:
command: systemctl start nginx
Properties:
InstanceType: t2.nano
ImageId: !Ref ImageId
NetworkInterfaces:
- AssociatePublicIpAddress: 'True'
DeleteOnTermination: 'True'
SubnetId: !Ref Subnet
DeviceIndex: '0'
GroupSet: [!GetAtt InstanceSecurityGroup.GroupId]
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -xe
yum install -y aws-cfn-bootstrap
yum update -y
amazon-linux-extras install nginx1.12 -y
curl --silent --location https://rpm.nodesource.com/setup_12.x | bash -
yum install git nodejs -y
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
Tags:
- Key: Name
Value: EC2FromCloudFormationCLI
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
Outputs:
InstanceId:
Description: InstanceId of the newly created EC2 instance
Value:
Ref: EC2Instance
AZ:
Description: Availability Zone of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- AvailabilityZone
PublicDNS:
Description: Public DNSName of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicDnsName
PublicIP:
Description: Public IP address of the newly created EC2 instance
Value:
Fn::GetAtt:
- EC2Instance
- PublicIp
AWSTemplateFormatVersion: 2010-09-09
Description: >-
This template creates two Amazon EC2 instances, and an Application Load Balancer.
Parameters:
KeyName:
Description: Name of an existing EC2 Key Pair
Type: AWS::EC2::KeyPair::KeyName
VPC:
Type: AWS::EC2::VPC::Id
Description: Choose which VPC that the Application Load Balancer should be deployed to
Subnets:
Description: Choose minimum of 2 subnets (2 different availability zones) that Application Load Balancer should be deployed to
Type: List<AWS::EC2::Subnet::Id>
Resources:
EC2Instance1:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
files:
/home/ec2-user/public/index.html:
content: !Sub |
<html><head><title>Simple Web App</title></head><body>Hello World!</body></html>
/home/ec2-user/package.json:
content: !Sub |
{"name":"simple-app","authors":"patrick","dependencies":{"express":"4.15.4"}}
/home/ec2-user/server.js:
content: !Sub |
var express = require('express'),
app = express(),
port = process.env.PORT || 80;
app.use(express.static(__dirname + '/public'));
app.listen(port, function(){
console.log('Server listening on port ', port) })
commands:
NPMInstall:
command: 'npm install'
cwd: '/home/ec2-user/'
RunWebserver:
command: 'node /home/ec2-user/server.js'
cwd: '/home/ec2-user/'
Properties:
InstanceType: t2.micro
ImageId: ami-0de53d8956e8dcf80 # Amazon Linux 2 in N. Virginia
Tags:
- Key: Name
Value: Webserver1
KeyName: !Ref KeyName
NetworkInterfaces:
- AssociatePublicIpAddress: 'True'
DeleteOnTermination: 'True'
SubnetId: !Select [0, !Ref Subnets]
DeviceIndex: '0'
GroupSet: [!GetAtt MySecurityGroup.GroupId]
UserData:
'Fn::Base64':
!Sub |
#!/bin/bash -xe
#Ensure AWS CFN Bootstrap is the latest
yum install -y aws-cfn-bootstrap
# Install Node.js from Official RPM
curl --silent --location https://rpm.nodesource.com/setup_11.x | bash -
yum -y install nodejs
echo 'Node.JS Installed'
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance1 --region ${AWS::Region}
EC2Instance2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-0de53d8956e8dcf80 # Amazon Linux 2 in N. Virginia
Tags:
- Key: Name
Value: Webserver2
KeyName: !Ref KeyName
NetworkInterfaces:
- AssociatePublicIpAddress: 'True'
DeleteOnTermination: 'True'
SubnetId: !Select [1, !Ref Subnets]
DeviceIndex: '0'
GroupSet: [!GetAtt MySecurityGroup.GroupId]
UserData:
'Fn::Base64':
!Sub |
#!/bin/bash -xe
#Ensure AWS CFN Bootstrap is the latest
yum install -y aws-cfn-bootstrap
# Install Node.js from Official RPM
curl --silent --location https://rpm.nodesource.com/setup_11.x | bash -
yum -y install nodejs
echo 'Node.JS Installed'
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance1 --region ${AWS::Region}
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22 and Enable Http via port 80
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0 # Use Testing only - open to all ip addresses
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: 'MyLoadBalancer1'
Subnets: !Ref Subnets
SecurityGroups: [!GetAtt MySecurityGroup.GroupId]
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ALBTargetGroup
LoadBalancerArn: !Ref ApplicationLoadBalancer
Port: 80
Protocol: HTTP
ALBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Protocol: HTTP
Port: 80
HealthCheckIntervalSeconds: 30
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 3
Matcher:
HttpCode: '200'
Name: MyTargets
Targets:
- Id:
Ref: EC2Instance1
Port: 80
- Id:
Ref: EC2Instance2
Port: 80
VpcId: !Ref VPC
Outputs:
SecurityGroup:
Description: Security Group
Value: !Ref MySecurityGroup
SecurityGroupId:
Description: Security Group ID
Value: !GetAtt MySecurityGroup.GroupId
LoadBalancer:
Description: A reference to the Application Load Balancer
Value: !Ref ApplicationLoadBalancer
LoadBalancerUrl:
Description: The URL of the ALB
Value: !GetAtt ApplicationLoadBalancer.DNSName
WebServer1Dns:
Description: EC2 Instance 1
Value: !GetAtt
- EC2Instance1
- PublicDnsName
WebServer2Dns:
Description: EC2 Instance 2
Value: !GetAtt
- EC2Instance2
- PublicDnsName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment