In Amazon Web Services (AWS), the need to manage multiple instances of Amazon Elastic Compute Cloud (EC2) instances effectively has led to the development of various tools to simplify the process. One such tool is the AWS Systems Manager (SSM), which enables users to manage EC2 instances, as well as other AWS resources, using a single interface. One of the most powerful features of SSM is the ability to perform SSH-less login to EC2 machines, which we will explore in this blog.
EC2DevIamRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-iam-role
Description: "SSM IAM EC2 Instance Role."
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
EC2DevInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: !Sub ${AWS::StackName}-instance-profile
Roles: [!Ref EC2DevIamRole]
PrivateServerInstance1:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref UbuntuAMIId
#ImageId: !Ref AMIId
IamInstanceProfile: !Ref EC2DevInstanceProfile
InstanceType: !Ref DefaultInstancdType
KeyName: !Ref KeyName
SubnetId: !Ref PrivateSubnet1
SecurityGroupIds:
- !Ref BaseSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
hostnamectl set-hostname privsvr01
apt update
apt upgrade
apt install -y wget
mkdir /tmp/ssm
cd /tmp/ssm
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
dpkg -i amazon-ssm-agent.deb
systemctl enable amazon-ssm-agent
systemctl start amazon-ssm-agent
Tags:
- Key: Name
Value: !Join [".", [privsvr01, !Ref paramUniqueName,!Ref DomainName]]
- Key: environment
Value: !Ref paramUniqueName
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
sudo dpkg -i session-manager-plugin.deb
sudo dnf install -y https://s3.amazonaws.com/session-manager-downloads/plugin/latest/linux_64bit/session-manager-plugin.rpm
aws ssm start-session --target i-05a0e2db2412424b7