|
1 | 1 |
|
2 |
| -# Example | Create an AWS VPC Network |
| 2 | +# Terraform Docker Example | Create an AWS VPC Network |
3 | 3 |
|
4 |
| -This example creates a VPC, subnets and the networking backbone to allow traffic to be routed in and also routed out to service endpoints on the internet. Let's first use docker then do the same thing with terraform installed on your machine. |
| 4 | +This example creates a VPC, subnets and the networking backbone to allow traffic to be routed in and also routed out to service endpoints on the internet. |
5 | 5 |
|
| 6 | +### Step 1 | git clone into docker volume |
6 | 7 |
|
7 |
| -## Docker | Create VPC Networks |
8 |
| - |
9 |
| -With docker, you need not worry about which Terraform version is installed on your machine. All you need are your AWS access credentials. |
10 |
| - |
| 8 | +First we create a docker volume (called **`vol.tfstate`**) and add the terraform module code to it by way of an **alpine git** container. |
11 | 9 |
|
12 | 10 | ```
|
13 |
| -docker build --rm --no-cache --tag devops4me/vpc-network . |
14 |
| -
|
15 |
| -### This Actually Works (But the next problem is - CAN WE DESTROY) |
16 |
| -### ALSO this prompts - we need to add -auto-approve to the docker file |
17 |
| -docker run -i -e AWS_DEFAULT_REGION=eu-west-1 -e AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX -e AWS_SECRET_ACCESS_KEY=XXXXXXX -e TF_VAR_in_role_arn=ZZZZZZZZZZZZ -t devops4me/vpc-network apply |
| 11 | +docker volume create vol.tfstate |
| 12 | +docker run --interactive \ |
| 13 | + --tty \ |
| 14 | + --rm \ |
| 15 | + --volume vol.tfstate:/terraform-work \ |
| 16 | + alpine/git \ |
| 17 | + clone https://github.com/devops4me/terraform-aws-vpc-network /terraform-work |
| 18 | +sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data |
| 19 | +``` |
18 | 20 |
|
| 21 | +**verify** - when you list the files in the container you will see the terraform module's contents there. |
19 | 22 |
|
20 | 23 |
|
| 24 | +### Step 2 | terraform init via docker |
21 | 25 |
|
| 26 | +As our volume contains the terraform module code from git we are now ready to perform a terraform init. We use the **[devops4me/terraform container](https://cloud.docker.com/repository/docker/devops4me/terraform/general)** container which adds a VOLUME mapping to the **[hashicorp/terraform](https://hub.docker.com/r/hashicorp/terraform/)** container at the **`/terraform-work`** location. |
22 | 27 |
|
| 28 | +``` |
| 29 | +docker run --interactive \ |
| 30 | + --tty \ |
| 31 | + --rm \ |
| 32 | + --name vm.terraform \ |
| 33 | + --volume vol.tfstate:/terraform-work \ |
| 34 | + devops4me/terraform init example |
| 35 | +sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data |
| 36 | +``` |
23 | 37 |
|
24 |
| -docker run -i -t devops4me/vpc-network \ |
25 |
| - --env AWS_DEFAULT_REGION=eu-west-1 apply |
| 38 | +**verify** - the directory listing now contains a **`.terraform`** directory. |
26 | 39 |
|
27 | 40 |
|
28 | 41 |
|
29 |
| -git clone github.com/devops4me/terraform-aws-vpc-network |
30 |
| -cd terraform-aws-vpc-network/example |
31 |
| -docker build --rm --no-cache --tag devops4me/vpc-network . |
32 |
| -docker images |
33 |
| -docker run \ |
34 |
| - --detach \ |
35 |
| - --name vm.vpc \ |
36 |
| - --network host \ |
37 |
| - --volume ${PWD}:/home/ubuntu \ |
38 |
| - devops4me/vpc-network; |
39 |
| -``` |
| 42 | +### Step 3 | terraform apply via docker |
40 | 43 |
|
41 |
| - |
42 |
| -## How to Run the Example |
| 44 | +At last we can run the terraform apply. Provide a **role arn** only if your organization works with roles alongside the other 3 AWS authentication keys. |
43 | 45 |
|
44 | 46 | ```
|
45 |
| -# get module and go to example directory |
46 |
| -git clone github.com/devops4me/terraform-aws-vpc-network |
47 |
| -cd terraform-aws-vpc-network/example |
48 |
| -
|
49 |
| -# export access information |
50 |
| -export TF_VAR_in_role_arn=<<role-arn>> |
51 |
| -export AWS_ACCESS_KEY_ID=<<access-key-id>> |
52 |
| -export AWS_SECRET_ACCESS_KEY=<<secret-access-key>> |
53 |
| -export AWS_DEFAULT_REGION=<<region-key>> |
54 |
| -
|
55 |
| -# use terraform to bring up and tear down infastructure |
56 |
| -terraform init |
57 |
| -terraform providers |
58 |
| -terraform apply -auto-approve |
59 |
| -terraform show |
60 |
| -terraform destroy -auto-approve |
| 47 | +docker run --interactive \ |
| 48 | + --tty \ |
| 49 | + --rm \ |
| 50 | + --name vm.terraform \ |
| 51 | + --env AWS_DEFAULT_REGION=<<aws-region-key>> \ |
| 52 | + --env AWS_ACCESS_KEY_ID=<<aws-access-key>> \ |
| 53 | + --env AWS_SECRET_ACCESS_KEY=<<aws-secret-key>> \ |
| 54 | + --env TF_VAR_in_role_arn=<<aws-role-arn>> \ |
| 55 | + --volume vol.tfstate:/terraform-work \ |
| 56 | + devops4me/terraform apply -auto-approve example |
| 57 | +sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data |
61 | 58 | ```
|
62 | 59 |
|
63 |
| -## Inputs |
64 |
| - |
65 |
| -| Input Variable | Type | Description | Required? | |
66 |
| -|:-------------------------- |:-------:|:------------------------------------------------------------- |:--------------:| |
67 |
| -| **in_role_arn** | String | Pass if using an IAM role as the AWS access mechanism. | optional | |
68 |
| - |
69 |
| -### What is the role arn? |
| 60 | +**verify** - the **docker volume** now has a **tfstate file** which documents the state of your infrastructure after terraform apply. |
70 | 61 |
|
71 |
| -If you are using an IAM role as the AWS access mechanism then pass it as in_role_arn commonly through an environment variable named **TF_VAR_in_role_arn** in addition to the usual AWS access key, secret key and default region parameters. |
72 | 62 |
|
73 |
| -Individuals and small businesses who don't have hundreds of AWS accounts can omit the variable and thanks to dynamic assignment the assume_role block will cease to exist. |
| 63 | +### Step 4 | terraform destroy via docker |
74 | 64 |
|
| 65 | +After running plan and apply either once or multiple times you may feel the need to **`terraform destroy`** the infrastructure. |
75 | 66 |
|
76 |
| -### The AWS 5 VPC's Limit |
77 |
| - |
78 |
| -The default VPC limit is just 5 and this test needs at least 10 so take yourself to the support section and request extension to say 25 - it will be done automatically in less than 5 minutes. |
| 67 | +``` |
| 68 | +docker run --interactive \ |
| 69 | + --tty \ |
| 70 | + --rm \ |
| 71 | + --name vm.terraform \ |
| 72 | + --env AWS_DEFAULT_REGION=<<aws-region-key>> \ |
| 73 | + --env AWS_ACCESS_KEY_ID=<<aws-access-key>> \ |
| 74 | + --env AWS_SECRET_ACCESS_KEY=<<aws-secret-key>> \ |
| 75 | + --env TF_VAR_in_role_arn=<<aws-role-arn>> \ |
| 76 | + --volume vol.tfstate:/terraform-work \ |
| 77 | + devops4me/terraform destroy -auto-approve example |
| 78 | +sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data |
| 79 | +``` |
79 | 80 |
|
| 81 | +**verify** - check your AWS console and also note that the volume now has a **tfstate backup file** created by terraform. |
0 commit comments