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 hidden or 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 = "autobots@ga.gov.au" | |
| } | |
| 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}" | |
| } |
Author
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 ComplianceSelect IAMAttach existing Policies DirectlySelect<appname>TerraformRunnerPolicy Documentfieldaccess key idandsecret access keyaws configureAWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYDownload and run using
terraform applyYou will see a
bucket_idanddynamodb_lock_table, this can be used in your terraform remote state configuration.