Created
August 21, 2017 00:27
-
-
Save tom-butler/b0e782545f0886f923ad2af994cdf9d9 to your computer and use it in GitHub Desktop.
Terraform remote-state-provisioner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================== | |
# remote-state.tf | |
#============================================================== | |
# This file is used to set variables that are passed to sub | |
# modules to build our stack | |
#-------------------------------------------------------------- | |
# Global Config | |
#-------------------------------------------------------------- | |
# Variables used in the global config | |
variable "region" { | |
description = "The AWS region we want to build this stack in" | |
default = "ap-southeast-2" | |
} | |
variable "owner" { | |
description = "A group email address to be used in tags" | |
default = "[email protected]" | |
} | |
provider "aws" { | |
region = "${var.region}" | |
} | |
# Data source used to retrieve the AWS account ID | |
data "aws_caller_identity" "current" {} | |
#-------------------------------------------------------------- | |
# Remote State Infrastructure | |
#-------------------------------------------------------------- | |
# Create the remote objects that terraform will use to store | |
# state - an S3 bucket and a DynamoDB table. | |
resource "aws_s3_bucket" "terraform_state" { | |
bucket = "tfstate-${data.aws_caller_identity.current.account_id}" | |
acl = "private" | |
tags { | |
Name = "terraform-state" | |
owner = "${var.owner}" | |
created_by = "remote-state-provisioner" | |
} | |
} | |
resource "aws_dynamodb_table" "terraform_statelock" { | |
name = "terraform-lock" | |
read_capacity = 20 | |
write_capacity = 20 | |
hash_key = "LockID" | |
attribute { | |
name = "LockID" | |
type = "S" | |
} | |
tags { | |
Name = "terraform-state-locking" | |
owner = "${var.owner}" | |
created_by = "remote-state-provisioner" | |
} | |
} | |
# Outputs | |
output "account_id" { | |
value = "${data.aws_caller_identity.current.account_id}" | |
} | |
output "bucket_id" { | |
value = "${aws_s3_bucket.terraform_state.id}" | |
} | |
output "dynamodb_lock_table" { | |
value = "${aws_dynamodb_table.terraform_statelock.id}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a service user to run the Terraform Scripts
Security, Identity and Compliance
Select IAMAttach existing Policies Directly
Select
<appname>TerraformRunner
Policy Document
fieldaccess key id
andsecret access key
aws configure
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
Download and run using
terraform apply
You will see a
bucket_id
anddynamodb_lock_table
, this can be used in your terraform remote state configuration.