Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added changes for Mod 5 #47

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions 05-ec2/ec2-5-1-2.yaml
@@ -0,0 +1,43 @@
"AWSTemplateFormatVersion": "2010-09-09"

Parameters:
WindowsAmi:
Type: String
LinuxAmi:
Type: String
instancetype:
Type: String

Resources:
MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
KeyName: fidelis
InstanceType: !Ref instancetype
TagSpecifications:
- ResourceType: instance
Tags:
- Key: user
Value: fidelis.ogunsanmi.labs
- Key: "stelligent-u-lesson"
Value: "5"


MyWindowsInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref WindowsAmi
AvailabilityZone: us-west-1a
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: "1"

MyLinuxInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LinuxAmi
AvailabilityZone: us-west-1a
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: "1"
59 changes: 59 additions & 0 deletions 05-ec2/ec2-5-2-1.yaml
@@ -0,0 +1,59 @@
"AWSTemplateFormatVersion": "2010-09-09"

Parameters:
WindowsAmi:
Type: String
LinuxAmi:
Type: String
instancetype:
Type: String

Resources:

MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Allen F Lab 5 SG"
GroupName: sglab521
SecurityGroupIngress:
- IpProtocol: icmp
FromPort: 8
ToPort: 0
CidrIp: 0.0.0.0/0

MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
InstanceType: !Ref instancetype
SecurityGroupIds:
- !GetAtt MySecurityGroup.GroupId
TagSpecifications:
- ResourceType: instance
Tags:
- Key: user
Value: fidelis.ogunsanmi.labs
- Key: "stelligent-u-lesson"
Value: "5"

MyLinuxInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LinuxAmi
AvailabilityZone: us-west-1a
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: "1"

MyEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref MyLinuxInstance52

Outputs:
EIP:
Description: "Public IP for Instance"
Value: !Ref MyEIP
Export:
Name: !Join ['', [!Ref "AWS::StackName", "-EIP" ]]
63 changes: 63 additions & 0 deletions 05-ec2/ec2-5-2-2.yaml
@@ -0,0 +1,63 @@
"AWSTemplateFormatVersion": "2010-09-09"

Parameters:
WindowsAmi:
Type: String
LinuxAmi:
Type: String
instancetype:
Type: String

Resources:

MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Allen F Lab 5 SG"
GroupName: sglab521
SecurityGroupIngress:
- IpProtocol: icmp
FromPort: 8
ToPort: 0
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0

MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
InstanceType: !Ref instancetype
SecurityGroupIds:
- !GetAtt MySecurityGroup.GroupId
TagSpecifications:
- ResourceType: instance
Tags:
- Key: user
Value: fidelis.ogunsanmi.labs
- Key: "stelligent-u-lesson"
Value: "5"

MyLinuxInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LinuxAmi
AvailabilityZone: us-west-1a
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: "1"

MyEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref MyLinuxInstance52

Outputs:
EIP:
Description: "Public IP for Instance"
Value: !Ref MyEIP
Export:
Name: !Join ['', [!Ref "AWS::StackName", "-EIP" ]]
77 changes: 77 additions & 0 deletions 05-ec2/ec2-5-3-2.yaml
@@ -0,0 +1,77 @@
"AWSTemplateFormatVersion": "2010-09-09"

Parameters:
WindowsAmi:
Type: String
LinuxAmi:
Type: String
instancetype:
Type: String

Resources:

MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Allen F Lab 5 SG"
GroupName: sglab521
SecurityGroupIngress:
- IpProtocol: icmp
FromPort: 8
ToPort: 0
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0

MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
InstanceType: !Ref instancetype
SecurityGroupIds:
- !GetAtt MySecurityGroup.GroupId
TagSpecifications:
- ResourceType: instance
Tags:
- Key: user
Value: fidelis.ogunsanmi.labs
- Key: "stelligent-u-lesson"
Value: "5"
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
apt-get update -y
apt-get install -y python-pip
apt-get install -y wget
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -O /tmp/amazon-cloudwatch-agent.deb
dpkg -i /tmp/amazon-cloudwatch-agent.deb
easy_install --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets default
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}



MyLinuxInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LinuxAmi
AvailabilityZone: us-west-1a
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
Version: "1"

MyEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref MyLinuxInstance52

Outputs:
EIP:
Description: "Public IP for Instance"
Value: !Ref MyEIP
Export:
Name: !Join ['', [!Ref "AWS::StackName", "-EIP" ]]
146 changes: 146 additions & 0 deletions 05-ec2/ec2-5-3-3.yaml
@@ -0,0 +1,146 @@
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Template to install CloudWatchAgent on amazon linux. It was validated on amazon linux 2'
Parameters:

instancetype:
Type: String

LinuxAmi:
Type: String

IAMRole:
Description: EC2 attached IAM role
Type: String
Default: CloudWatchAgentAdminRole

SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

Resources:
EC2Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- 01_setupCfnHup
- 02_config-amazon-cloudwatch-agent
- 03_restart_amazon-cloudwatch-agent
UpdateEnvironment:
- 02_config-amazon-cloudwatch-agent
- 03_restart_amazon-cloudwatch-agent
# Definition of json configuration of AmazonCloudWatchAgent, you can change the configuration below.
02_config-amazon-cloudwatch-agent:
files:
'/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json':
content: !Sub |
{
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${!aws:AutoScalingGroupName}",
"ImageId": "${!aws:ImageId}",
"InstanceId": "${!aws:InstanceId}",
"InstanceType": "${!aws:InstanceType}"
},
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
]
},
"swap": {
"measurement": [
"swap_used_percent"
]
}
}
}
}
# Invoke amazon-cloudwatch-agent-ctl to restart the AmazonCloudWatchAgent.
03_restart_amazon-cloudwatch-agent:
commands:
01_stop_service:
command: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop
02_start_service:
command: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s
# Cfn-hup setting, it is to monitor the change of metadata.
# When there is change in the contents of json file in the metadata section, cfn-hup will call cfn-init to restart the AmazonCloudWatchAgent.
01_setupCfnHup:
files:
'/etc/cfn/cfn-hup.conf':
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
'/etc/cfn/hooks.d/amazon-cloudwatch-agent-auto-reloader.conf':
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init.02_config-amazon-cloudwatch-agent
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets UpdateEnvironment
runas=root
mode: '000400'
owner: root
group: root
"/lib/systemd/system/cfn-hup.service":
content: !Sub |
[Unit]
Description=cfn-hup daemon
[Service]
Type=simple
ExecStart=/opt/aws/bin/cfn-hup
Restart=always
[Install]
WantedBy=multi-user.target
commands:
01enable_cfn_hup:
command: !Sub |
systemctl enable cfn-hup.service
02start_cfn_hup:
command: !Sub |
systemctl start cfn-hup.service

Properties:
InstanceType: !Ref instancetype
IamInstanceProfile: !Ref IAMRole
KeyName: fidelis
ImageId: !Ref LinuxAmi
SecurityGroupIds:
- !GetAtt MySecurityGroup.GroupId
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
apt-get update -y
apt-get install -y python-pip
apt-get install -y wget
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -O /tmp/amazon-cloudwatch-agent.deb
dpkg -i /tmp/amazon-cloudwatch-agent.deb
easy_install --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets default
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}

CreationPolicy:
ResourceSignal:
Count: 1
Timeout: "PT15M"

MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref SSHLocation