- Newest
- Most votes
- Most comments
Hi John,
Here are the steps you need to follow:
a. Create a EC2 service role with *AmazonSSMFullAccess
b. Attach this role to ec2, go to Actions -> Security -> Modify IAM Role
c. Make sure, security group attached to EC2 instance, allow inbound traffic from your machine(for testing purpose you can allow All traffic, anywhere -not recommended though, and remove after testing)
d. After installing the SSM agent on ec2, follow these steps:
- aws ec2-instance-connect send-ssh-public-key --instance-id $instance_id --instance-os-user ec2-user --availability-zone us-east-1d --ssh-public-key file:///Users/abc/.ssh/id_rsa.pub --region us-east-1 --profile <awscli_profile_name>
- aws ssm send-command --instance-ids $instance_id --document-name "AWS-RunShellScript" --parameters '{"commands":["sudo su"]}' --region us-east-1 --profile <awscli_profile_name> --output text
- aws ssm send-command --instance-ids $instance_id --document-name "AWS-RunShellScript" --parameters '{"commands":["cd /"]}' --region us-east-1 --profile <awscli_profile_name> --output text
- aws ssm send-command --instance-ids $instance_id --document-name "AWS-RunShellScript" --parameters '{"commands":["ls"]}' --region us-east-1 --profile <awscli_profile_name> --output text
Make sure, in step-1, you pass your local machine ssh public key, from where you intend to connect to ec2 instance.
I'm attaching snapshot for your reference:
Here is the script version of same attempt:
1. Script content and it's output:
2. File which is present at root directory on EC2 instance and I'm doing 'cat' through SSM as mentioned above in the script content:
3. Finally the command output from SSM console to verify it's working as expected:
Hope you find this helpful, let me know how it works for you.
I tried and got the following message:
Connecting to Account (AWS). . . Connection established to account ( AWS ).
{ "RequestId": "xxxxxxxxxxxxxxxxxxxxx", "Success": true }
An error occurred (InvalidInstanceId) when calling the SendCommand operation: Instances [[i-xxxxxxxxxxxxxxxxxx]] not in a valid state for account xxxxxxxxxxxxxxxxxx
Glad to know that you are one step closer, I also faced this issue for couple of times. Can you make sure of following:
-
Role which is attached to EC2 has AmazonSSMFullAccess and trust relationship for ec2.amazonaws.com with action sts:AssumeRole
-
Instance have SSM agent installed and running(refer doc) and make sure you installed the right agent based on type of ec2 machine.
sudo systemctl status amazon-ssm-agent
-
Add security group allowing inbound traffic, after adding ALL Traffic/Anywhere(for testing purpose), waited for few minutes, then ran the script and worked.
I can say from here that you are now in right direction. Keep me posted.
-
Hi John, Any update here, how it went. I'd like to hear from you, feel free to comment here if you are still having issues or have any additional questions. If this answers your question, please approve the answer for better community experience.
Relevant content
- asked a year ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 months ago
Hi John, Any update here, how it went. I'd like to hear from you, feel free to comment here if you are still having issues or have any additional questions. If this answers your question, please approve the answer for better community experience.