0
votes

How do I enable the AWS CLI on an EC2 instance? After I create the EC2 instance, I can SSH into the machine, but when I try to do something like aws s3 ls, it prompts me to do aws configure first, which I then have to enter my keys. I want to be able to automate this so that I can grab additional artifacts from S3 buckets to install. Note that I am using the AWS CLI on my computer to create the EC2 instance, but I need to use the AWS CLI on the EC2 instance itself.

My AWS command to create a simple EC2 instance looks like the following (this is done on my computer).

aws ec2 run-instances \
 --image-id ami-14c5486b \
 --count 1 \
 --instance-type t2.micro \
 --key-name testkey \
 --subnet-id subnet-xxxxxxxx \
 --security-group-ids sg-xxxxxxxx \
 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test}]'
 --user-data file://install-software.sh

The install-software.sh looks something like the following (this is submitted to the EC2 instance).

#!/bin/bash

aws s3 cp s3://mybucket/some-archive.tar.gz some-archive.tar.gz
tar xf some-archive.tar.gz
sudo some-archive/bin/install.sh
2

2 Answers

2
votes

You need to use an instance profile when launching your EC2 instance – if it has an instance profile attached then the AWS CLI will automatically use the permissions set in it to grant access to resources, rather than relying on your providing credentials.

0
votes

You need to assign an instance role to your instance. Give it rights to get objects from your bucket. Then the aws cli will get the credentials from instance metadata automatically so you won't need to configure aws first.