Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active April 9, 2023 03:44
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 gilangvperdana/83f7e7262244cc2519fbcf71e121fabe to your computer and use it in GitHub Desktop.
Save gilangvperdana/83f7e7262244cc2519fbcf71e121fabe to your computer and use it in GitHub Desktop.
Example of AWS CloudFormation
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Application Template: Demonstrates how to reference resources from a different stack.
This template provisions an EC2 instance in a VPC Subnet provisioned in a different stack.
# This template creates:
# Amazon EC2 instance
# Security Group
######################
# Parameters section
######################
Parameters:
NetworkStackName:
Description: >-
Name of an active CloudFormation stack that contains the networking
resources, such as the VPC and subnet that will be used in this stack.
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
Default: lab-network
AmazonLinuxAMIID:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
######################
# Resources section
######################
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
configSets:
All:
- ConfigureSampleApp
ConfigureSampleApp:
packages:
yum:
httpd: []
files:
/var/www/html/index.html:
content: |
<img src="https://s3.amazonaws.com/cloudformation-examples/cloudformation_graphic.png" alt="AWS CloudFormation Logo"/>
<h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1>
mode: 000644
owner: apache
group: apache
services:
sysvinit:
httpd:
enabled: true
ensureRunning: true
Properties:
InstanceType: t2.micro
ImageId: !Ref AmazonLinuxAMIID
NetworkInterfaces:
- GroupSet:
- !Ref WebServerSecurityGroup
AssociatePublicIpAddress: true
DeviceIndex: 0
DeleteOnTermination: true
SubnetId:
Fn::ImportValue:
!Sub ${NetworkStackName}-SubnetID
Tags:
- Key: Name
Value: Web Server
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets All --region ${AWS::Region}
# Signal the status from cfn-init
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerInstance --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT5M
DiskVolume:
Type: AWS::EC2::Volume
Properties:
Size: 100
AvailabilityZone: !GetAtt WebServerInstance.AvailabilityZone
Tags:
- Key: Name
Value: Web Data
DeletionPolicy: Snapshot
DiskMountPoint:
Type: AWS::EC2::VolumeAttachment
Properties:
InstanceId: !Ref WebServerInstance
VolumeId: !Ref DiskVolume
Device: /dev/sdh
WebServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP ingress
VpcId:
Fn::ImportValue:
!Sub ${NetworkStackName}-VPCID
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: Web Server Security Group
######################
# Outputs section
######################
Outputs:
URL:
Description: URL of the sample website
Value: !Sub 'http://${WebServerInstance.PublicDnsName}'
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Application Template: Demonstrates how to reference resources from a different stack.
This template provisions an EC2 instance in a VPC Subnet provisioned in a different stack.
# This template creates:
# Amazon EC2 instance
# Security Group
######################
# Parameters section
######################
Parameters:
NetworkStackName:
Description: >-
Name of an active CloudFormation stack that contains the networking
resources, such as the VPC and subnet that will be used in this stack.
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
Default: lab-network
AmazonLinuxAMIID:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
######################
# Resources section
######################
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
configSets:
All:
- ConfigureSampleApp
ConfigureSampleApp:
packages:
yum:
httpd: []
files:
/var/www/html/index.html:
content: |
<img src="https://s3.amazonaws.com/cloudformation-examples/cloudformation_graphic.png" alt="AWS CloudFormation Logo"/>
<h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1>
mode: 000644
owner: apache
group: apache
services:
sysvinit:
httpd:
enabled: true
ensureRunning: true
Properties:
InstanceType: t2.micro
ImageId: !Ref AmazonLinuxAMIID
NetworkInterfaces:
- GroupSet:
- !Ref WebServerSecurityGroup
AssociatePublicIpAddress: true
DeviceIndex: 0
DeleteOnTermination: true
SubnetId:
Fn::ImportValue:
!Sub ${NetworkStackName}-SubnetID
Tags:
- Key: Name
Value: Web Server
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets All --region ${AWS::Region}
# Signal the status from cfn-init
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerInstance --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT5M
DiskVolume:
Type: AWS::EC2::Volume
Properties:
Size: 100
AvailabilityZone: !GetAtt WebServerInstance.AvailabilityZone
Tags:
- Key: Name
Value: Web Data
DeletionPolicy: Snapshot
DiskMountPoint:
Type: AWS::EC2::VolumeAttachment
Properties:
InstanceId: !Ref WebServerInstance
VolumeId: !Ref DiskVolume
Device: /dev/sdh
WebServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP ingress
VpcId:
Fn::ImportValue:
!Sub ${NetworkStackName}-VPCID
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: Web Server Security Group
######################
# Outputs section
######################
Outputs:
URL:
Description: URL of the sample website
Value: !Sub 'http://${WebServerInstance.PublicDnsName}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment