Skip to content

Commit 71bb3f9

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
create target groups demo added
1 parent 3b3ac1f commit 71bb3f9

12 files changed

Lines changed: 238 additions & 2 deletions

File tree

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,98 @@
11
# User Data
22

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
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Demo
2+
3+
We're going to SSH into web1 and deploy the web front end using `Docker`. We're then going to browse directly to web1's public IP address and verify that the web front end is actually working.
4+
5+
- SSH into `web1`
6+
- Deploy the front end using Docker
7+
- Browse to web1's public IP address
8+
9+
## Deploying the Web
10+
11+
Now, just so you're not taken by surprise, let's look at the Docker command we're going to run. You don't need to understand this in any detail, but there are a couple of things I want to point out.
12+
13+
```bash
14+
sudo docker run -d \
15+
-p 80:80 -p 443:443 \
16+
-h web1 \
17+
benpiper/mtwa:web
18+
```
19+
20+
```bash
21+
docker run -d -p 80:3000 -h web1 jaimesalas/todo-app-front:4
22+
```
23+
24+
Let's go ahead and SSH into the `web1` instance and run this.
25+
26+
- Connect to your instance
27+
28+
```bash
29+
ssh -i "your_key" your_instance
30+
```
31+
32+
Here we are on the `web1` instance, and the first thing we're going to do is spin up the Docker container for the web front end. So we're going to go ahead and enter the command for that.
33+
34+
```bash
35+
docker run -d -p 80:3000 -h web1 jaimesalas/todo-app-front:4
36+
```
37+
38+
Let's go ahead and grab the instance's public IP address. We'll do a curl `ifconfig.me`.
39+
40+
```bash
41+
curl ifconfig.me
42+
```
43+
44+
Let's jump over to our browser and then paste the IP address into the address bar and hit Enter.
45+
46+
> Copy public IP on browser
47+
48+
Now the front end of the web app is configured to display information about the client, web server, and application server.
49+
50+
Open dev tools, the cookie information is also blank because we, the client, have not sent any cookie.
51+
52+
> Browse using `https://<your ip>`
81.8 KB
Loading
130 KB
Loading
86.6 KB
Loading
32.9 KB
Loading
74.9 KB
Loading
117 KB
Loading
91.8 KB
Loading
102 KB
Loading

0 commit comments

Comments
 (0)