|
1 | 1 | # User Data |
2 | 2 |
|
3 | | -Lets |
| 3 | +Lets start by creating a script that will install Docker on the instance type that we're using: |
| 4 | + |
| 5 | +Create `install-docker.txt` |
| 6 | + |
| 7 | +```bash |
| 8 | +#!/bin/bash |
| 9 | +sudo dnf update |
| 10 | +sudo dnf install docker -y |
| 11 | +sudo systemctl start docker |
| 12 | +sudo systemctl enable docker |
| 13 | +sudo usermod -aG docker $USER |
| 14 | +newgrp docker |
| 15 | +``` |
| 16 | + |
| 17 | +If we want to start a new instance with user data we can do as follows: |
| 18 | + |
| 19 | +```bash |
| 20 | +aws ec2 run-instances --image-id <image id> --count 1 --instance-type <instance type> \ |
| 21 | +--key-name my-key-pair --subnet-id <subnet id> --security-group-ids <sg-id> \ |
| 22 | +--user-data file://my_script.txt |
| 23 | +``` |
| 24 | + |
| 25 | +We're going to use the Ireland region and the default vpc and subnet. So lets try to grab the values that we need to do this: |
| 26 | + |
| 27 | +```bash |
| 28 | +VPC=$(aws ec2 describe-vpcs --region eu-west-1 | jq -r '.Vpcs[0]."VpcId"') |
| 29 | +``` |
| 30 | + |
| 31 | +Lets also create a security that allows SSH Traffic |
| 32 | + |
| 33 | +```bash |
| 34 | +SSHSG=$(aws ec2 create-security-group \ |
| 35 | +--group-name ssh-sg \ |
| 36 | +--description "ssh-sg" \ |
| 37 | +--region eu-west-1 \ |
| 38 | +--vpc-id $VPC | jq -r '.GroupId') |
| 39 | +``` |
| 40 | + |
| 41 | +```bash |
| 42 | +aws ec2 authorize-security-group-ingress \ |
| 43 | + --group-id $SSHSG \ |
| 44 | + --region eu-west-1 \ |
| 45 | + --ip-permissions '[{"IpProtocol":"tcp","FromPort": 22,"ToPort": 22,"IpRanges": [{"CidrIp":"0.0.0.0/0"}]}]' |
| 46 | +``` |
| 47 | + |
| 48 | +For last we need a subnet on default VPC, lets grab one: |
| 49 | + |
| 50 | +```bash |
| 51 | +SUBNET=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPC" \ |
| 52 | +--region eu-west-1 | jq -r '.Subnets[0]."SubnetId"') |
| 53 | +``` |
| 54 | + |
| 55 | +We need to create a key in order to access our new instance, go to dashboard and create `dublin_key` |
| 56 | + |
| 57 | +With this we're finally ready to try to start our instance with user data: |
| 58 | + |
| 59 | +> NOTE: We have taken the image id from region AMI Catalog |
| 60 | +
|
| 61 | +```bash |
| 62 | +aws ec2 run-instances --image-id ami-07355fe79b493752d \ |
| 63 | + --count 1 --instance-type t3.micro \ |
| 64 | + --key-name dublin_key --subnet-id $SUBNET --security-group-ids $SSHSG \ |
| 65 | + --region eu-west-1 \ |
| 66 | +--user-data file://install-docker.txt |
| 67 | +``` |
| 68 | + |
| 69 | +Now lets conenct to instance: |
| 70 | + |
| 71 | +```bash |
| 72 | +ssh -i "dublin_key.pem" ec2-user@ec2-54-78-159-28.eu-west-1.compute.amazonaws.com |
| 73 | +``` |
| 74 | + |
| 75 | +If we just simply run `docker` we will see the list of available commands. But if we try any command with out sudo, we will get an error. |
| 76 | + |
| 77 | +```bash |
| 78 | +docker ps |
| 79 | +``` |
| 80 | + |
| 81 | +We can check that docker is running, by checking the service status: |
| 82 | + |
| 83 | +```bash |
| 84 | +sudo systemctl status docker |
| 85 | +``` |
| 86 | + |
| 87 | +The reason for that, is that the $USER during installation, is not the same one that we are using when we're connected via SSH, lets fix that: |
| 88 | + |
| 89 | +```bash |
| 90 | +sudo usermod -aG docker $USER |
| 91 | +newgrp docker |
| 92 | +``` |
| 93 | + |
| 94 | +Now if we run `docker ps` everything looks good. |
| 95 | + |
| 96 | +## References |
| 97 | + |
| 98 | +- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts |
0 commit comments