aws ssm automation


This blog will help you to deploy bash scripts or run linux commands on multiple servers at once.


Steps involved 

1. Create multiple EC2 machines 

2. Create IAM role for EC2 and attach SSM policy. 

3. Attach IAM role created in step 2 to EC2 machine 

4. Check in AWS SSM console if servers are showing or not in RUN command. 

5. Go to AWS SSM - select Run command and than select Shell script to run command on multiple linux machine at once 

6. Enter the bash script in the console 

7. Select the logs destination 

8. Deploy - check the in the console for status 



1. Create multiple EC2 machine 



Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
Choose Launch Instance.
Choose an Amazon Machine Image (AMI), find an Amazon Linux 2 AMI at the top of the list and choose Select.
Choose an Instance Type, choose Next: Configure Instance Details.
Configure Instance Details, provide the following information:
Leave Number of instances at one.
Leave Purchasing option at the default setting.
For Network, choose the entry for the Default VPC
For Subnet, choose a default subnet in any Availability Zone.
Choose Next: Add Storage.
For File systems, choose EBS or leave the default option
Choose Next: Add Tags.
Name your instance and choose Next: 
Configure Security Group.
Configure Security Group, select port number 22 and port 80 in inbound and leave the outbound port as it is (port 80 require to check the apache server)
Select the keypair - create new keypair - name as per choice, download the keypair
Choose Review and Launch.
Choose Launch.


2. Create IAM role for EC2 and attach SSM policy. 


Open the IAM console at https://console.aws.amazon.com/iam/.
In the navigation pane, choose Roles, and then choose Create role.
Select EC2 and than click on next permission 
Search SSMfullaccess policy from the list 
Name your Role - for eg. ec2role-ssm
Finish 


3. Attach IAM role created in step 2 to EC2 machine 

go to the EC2 machine created in the step 1.
from the console - ec2 - select the machine - go to action - security - select IAM role 
attach the IAM role created in step 2 for eg. ec2role-ssm
save the settings 


4. Check in AWS SSM console if servers are showing or not in Run command. 

go to aws console - services - search for ssm 
select Run command from the left side menu 
look-up for the instance attached with SSM role 

If instance is not showing - check step 2 and step 3 again 
if still problem make sure to stop and start the machine 


5. Go to AWS SSM - select Run command and than select Shell script to run command on multiple linux machine at once 

go to aws console - services - search for ssm 
select Run command from the left side menu 
search for AWS-RunShellScript




6. Enter the bash script in the console 



click on run command 
select description 
select document version 
now in command parameter enter the shell script 
---this script will install apache server------


#!/bin/bash
yum install httpd -y
service httpd start
chkconfig httpd on
echo "aws ssm demo to deploy on multiple machines" > /var/www/html/index.html


leave working directory and execution timeout as default
in the Target select the machine manually 
In other parameter - update comments 


7. Select the logs destination 

enable s3 bucket for logs 
select the s3 bucket name 
enable cloudwatch logs 

8. Deploy - check the in the console for status 


click run and wait for the status 
if status is showing success 
copy the ec2 public address and paste in the browser to verify apache installed or not 


Troubleshoot

Make sure in the security group port number 22 and port number 80 allow in inbond
check the IAM permission for correct access
check for the S3 bucket permission 


-----------Ref script to check if apache service running or not --------------

#!/usr/bin/env bash
set -e

echo "Starting httpd..."
if [ -x "$(command -v systemctl)" ]; then
  echo "using systemctl"
  sudo systemctl enable httpd.service
  sudo systemctl start httpd
else
  echo "using upstart"
  sudo start httpd
fi

Comments

Popular posts from this blog

aws kms key with s3 - how to use aws kms key to encrypt data - aws kms s3 demo

aws-ecs demo | amazon elastic container service demo | aws container demo | aws fargate