Skip to content

Commit 340a11f

Browse files
authored
218 terraform lockfiles use s3 not dynamodb (#219)
* Use S3 native state locking instead of DynamoDB (#218) Replace the deprecated dynamodb_table backend parameter with use_lockfile = true, drop the DynamoDB statement from the live IncubatorTfPlanSecretsRead policy, and remove the lock-table setup from CONTRIBUTING.md and the pre-work issue template. The backend.tf comment makes the terraform-plan/apply workflows trigger, since they only filter on **/*.tf. * Clarify changes migrating lockfiles to s3 from dynamodb Continues #218
1 parent a88c76a commit 340a11f

5 files changed

Lines changed: 6 additions & 36 deletions

File tree

‎.github/ISSUE_TEMPLATE/pre-work-template-devops-security.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ As a new member on the HfLA devops-security team, fill in the following fields a
3535
- [ ] [Generating user access keys](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#generating-access-keys-for-aws-cli)
3636
- [ ] Complete the instructions in [AWS Documentation](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html) and choose your operating system to install AWS CLI.
3737
- [ ] Complete the instruction in [AWS Documentation](https://docs.aws.amazon.com/cli/latest/userguide/cli-authentication-short-term.html) to setup the AWS CLI.
38-
- [ ] Follow the instructions in [Creating a backend state](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#creating-backend-state) to create the S3 bucket and DynamoDB table.
39-
- [ ] Create the S3 bucket
40-
- [ ] Create the DynamoDB table
38+
- [ ] Follow the instructions in [Creating a backend state](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#creating-backend-state) to create the S3 bucket.
39+
- [ ] Create the S3 bucket
4140
- [ ] Install Terraform locally by following the instructions of the installation guide mentioned in [Installing Terraform](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#installing-terraform)
4241
- [ ] Install Terraform Docs locally by following the instructions of the installation guide mentioned in [Installing Terraform docs](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#installing-terraform-docs)
4342
- [ ] Complete the instructions in [Clone the repository](https://github.com/hackforla/devops-security/blob/main/CONTRIBUTING.md#clone-the-repository)
@@ -78,7 +77,7 @@ Note: You must be authenticated to your AWS account via the CLI for the next com
7877
If you are unable to authenticate from your local machine using the CLI, post in the #ops channel in Slack so that the team can help you get unblocked.
7978
- [ ] Execute the command `terraform init` to initialize terraform in the directory. Address any failures that arise (if any).
8079
- [ ] Execute the command `terraform plan` this will output a plan replicating the same IAM resources as the devops security account. Address any failures that arise (if any).
81-
- [ ] Then execute the command `terraform apply` this will create all of the resources that are currently managed by Devops Security. All of the resources created here incur zero cost except for the Dynamo DB installation, which should remain in the free tier. **
80+
- [ ] Then execute the command `terraform apply` this will create all of the resources that are currently managed by Devops Security. All of the resources created here incur zero cost.
8281
- [ ] **If you have cost concerns, Run a Terraform Destroy to take down all of the resources you created (don't worry, you can recreate them just as quickly). If you create resources outside of what's described in this issue, you may incur charges.**
8382
- [ ] Update the README using Terraform Docs to document changes
8483
- [ ] ```terraform-docs -c .terraform.docs.yml .```

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,6 @@ aws s3api put-bucket-encryption --bucket USERNAME-hfla-ops-terraform-state --ser
154154
}'
155155
```
156156

157-
#### Set up DynamoDB to store the backend state
158-
159-
- Create table `hfla_ops_terraform_table`
160-
- Set partition key to `LockID` with a type of `string`
161-
- Choose on-demand capacity
162-
163-
You can create the table from the AWS UI, CloudShell, or from the CLI using the following command:
164-
165-
```bash
166-
aws dynamodb create-table \
167-
--table-name hfla_ops_terraform_table \
168-
--attribute-definitions AttributeName=LockID,AttributeType=S \
169-
--key-schema AttributeName=LockID,KeyType=HASH \
170-
--billing-mode PAY_PER_REQUEST
171-
```
172-
173157
<sub>[Back to Top](#)</sub>
174158
***
175159

@@ -248,7 +232,7 @@ After creating a backend state, create a `backend.tfvars` file in the `terraform
248232
bucket = "USERNAME-hfla-ops-terraform-state"
249233
key = "devops-security/terraform.tfstate"
250234
region = "us-west-2"
251-
dynamodb_table = "hfla_ops_terraform_table"
235+
use_lockfile = true
252236
encrypt = true
253237
```
254238

‎terraform/aws-custom-policies/tf-plan-scoped.json‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@
2525
"arn:aws:s3:::hfla-ops-terraform-state",
2626
"arn:aws:s3:::hfla-ops-terraform-state/*"
2727
]
28-
},
29-
{
30-
"Sid": "AllowDynamoDBLockTableAccessForTerraformPlan",
31-
"Effect": "Allow",
32-
"Action": [
33-
"dynamodb:PutItem",
34-
"dynamodb:DeleteItem",
35-
"dynamodb:GetItem",
36-
"dynamodb:UpdateItem",
37-
"dynamodb:DescribeTable"
38-
],
39-
"Resource": [
40-
"arn:aws:dynamodb:us-west-2:035866691871:table/hfla_ops_terraform_table"
41-
]
4228
}
4329
]
4430
}

‎terraform/backend.tf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ terraform {
44
// CI silently upgraded itself -- state has been written by 1.7 through 1.16.
55
// This line alone chooses the CI Terraform version.
66
required_version = "~> 1.16.0"
7+
// Locking config lives in terraform/prod.backend.tfvars (use_lockfile).
78
backend "s3" {
89
}
910
required_providers {

‎terraform/prod.backend.tfvars‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
bucket = "hfla-ops-terraform-state"
22
key = "devops-security/terraform.tfstate"
33
region = "us-west-2"
4-
dynamodb_table = "hfla_ops_terraform_table"
4+
use_lockfile = true
55
encrypt = true

0 commit comments

Comments
 (0)