iTranslated by AI
[CloudFormation] Template to Install Apache on EC2 Launch
Hello, I'm Tomada.
I've been into CloudFormation lately, so I'm sharing a template that uses User Data to initially install Apache on EC2, as introduced in the previous article below.
→How to initially install Apache using UserData when creating EC2
Also, the template introduced in this article is available on GitHub as well.
How to Configure User Data in a Template
When specifying an EC2 resource, you set it by including a property called UserData within the Properties.
In the example below, I have configured an initial script that performs everything from installing Apache to starting it and setting it to start automatically, as well as setting a string in index.html.
EC2:
Type: AWS::EC2::Instance
Properties:
...
UserData:
Fn::Base64: |
#!/bin/bash
yum -y update
yum -y install httpd
systemctl start httpd
systemctl enable httpd
echo "Apache is runnning by using UserData" > /var/www/html/index.html
What Can Be Created with This Template
- VPC x1
- Subnet x1
- EC2 x1 (Apache running)
Full Template
cf-ec2-userdata-apache-install.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: Provision EC2
Parameters:
EnvironmentName:
Description: Name which you can specify the environment by this name
Type: String
Default: test-environment
VpcCIDR:
Type: String
Default: 10.0.0.0/16
PublicSubnetCIDR:
Type: String
Default: 10.0.0.0/24
KeyName:
Description: The EC2 Key Pair to allow SSH access to the instance
Type: "AWS::EC2::KeyPair::KeyName"
MyIP:
Description: IP address range which is allowed to access this EC2 from it
Type: String
Default: 0.0.0.0/0
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-VPC
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-InternetGateway
# Attach InternetGateway to VPC
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1a
VpcId: !Ref VPC
CidrBlock: !Ref PublicSubnetCIDR
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-PublicSubnet
PublicSubnetRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-PublicSubnetRouteTable
# Routing from PublicSubnet to Internet
PublicSubnetToInternet:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicSubnetRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
# Associate PublicSubnetRouteTable to PublicSubnet
AssoPublicSubnetRouteTable:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet
RouteTableId: !Ref PublicSubnetRouteTable
EC2:
Type: AWS::EC2::Instance
Properties:
# Amazon Linux 2
ImageId: ami-00d101850e971728d
KeyName: !Ref KeyName
InstanceType: t2.micro
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
SubnetId: !Ref PublicSubnet
GroupSet:
- !Ref EC2SecurityGroup
UserData:
Fn::Base64: |
#!/bin/bash
yum -y update
yum -y install httpd
systemctl start httpd
systemctl enable httpd
echo "Apache is runnning by using UserData" > /var/www/html/index.html
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-EC2
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: ec2-sg-cf
GroupDescription: Allow SSH and HTTP access from only MyIP
VpcId: !Ref VPC
SecurityGroupIngress:
# http
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref MyIP
# ssh
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref MyIP
Access Test to the Web Server
When you access the automatically assigned public IP in your browser, you should see that the string specified in the user data is displayed.

Discussion