Skip to content

Commit 99468b6

Browse files
committed
the first dockerized example has now been released
1 parent 8573649 commit 99468b6

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

example/README.md

+56-54
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,81 @@
11

2-
# Example | Create an AWS VPC Network
2+
# Terraform Docker Example | Create an AWS VPC Network
33

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.
55

6+
### Step 1 | git clone into docker volume
67

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.
119

1210
```
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+
```
1820

21+
**verify** - when you list the files in the container you will see the terraform module's contents there.
1922

2023

24+
### Step 2 | terraform init via docker
2125

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.
2227

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+
```
2337

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.
2639

2740

2841

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
4043

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.
4345

4446
```
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
6158
```
6259

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.
7061

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.
7262

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
7464

65+
After running plan and apply either once or multiple times you may feel the need to **`terraform destroy`** the infrastructure.
7566

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+
```
7980

81+
**verify** - check your AWS console and also note that the volume now has a **tfstate backup file** created by terraform.

template_for_flow_logs.txt

-4
This file was deleted.

0 commit comments

Comments
 (0)