Bootstrap your AWS account to start managing it with Terraform.
Given that this repo focuses on new AWS accounts that do not yet use Terraform, this repo is meant to be forkable / copy-pastable instead of being invoked as a module.
This repo uses Terraform version 0.11.14
.
Terraform creates the AWS resources listed below.
- IAM
- A user with
AdministratorAccess
policy
- A user with
- DynamoDB
- A table for tracking Terraform usage. This is a Terraform feature that ensure that no more than one
plan
orapply
operations are running at the same time.
- A table for tracking Terraform usage. This is a Terraform feature that ensure that no more than one
- S3
- A bucket for storing Terraform state.
- In order to avoid potential permission issues when writing a different AWS account, the
bucket-owner-full-control
ACL is applied to the bucket.
- VPC
- Uses the AWS VPC Terraform module, with most of the defaults.
- One of the reasons to create a new VPC is to have control over the CIDR range, e.g. when designating all traffic as internal based on destination CIDR
10.x.x.x/8
. - You will likely want to set
enable_nat_gateway = false
. I disabled it because I'm not running any servers and theNatGateway-Hours
is costing me.
-
Fork this repo.
-
In AWS Console, create the S3 bucket where Terraform will its state. Terraform will need this bucket created beforehand, otherwise it will throw an
AccessDenied
error. -
Export your AWS root user's key id and secret into
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
, respectively. -
Run
terraform init
. -
Import the state bucket into Terraform
terraform import aws_s3_bucket.bucket bucket-name
-
Create a
terraform.tfvars
file with the following variables:aws_account_id = "YOUR_AWS_ACCOUNT_ID" admin_user = "YOUR_USERNAME" region = "YOUR_AWS_REGION" tags = { Owner = "YOUR_USERNAME" Environment = "production" # more tags as desired }
-
Run
terraform apply
.
After running terraform apply
ti create the resources using the AWS root user, do not use the root account anymore. Store the root user key id and secret in a safe location and use it in case IAM access fails.
Use the new IAM admin user created in this Terraform script to all future Terraform uses.
In my case I export that user's key and secret into AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables. If you can, it is recommended that you use the aws-vault project to store your role information to access your AWS account without exposing your user's key and secret. In this project, I still export key id and secret because I work in AWS accounts that use the aws-okta project, and its config conflicts with aws-vault
.
If you're using a different version of Terraform Switch between Terraform versions using tfswitch.
When using AWS roles instead of AWS users (recommended), create aws_iam_role
and aws_iam_role_policy_attachment
, instead of aws_iam_user
and aws_iam_user_policy_attachment
.