Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spencershepard/70610283a92e33713cca010b2caf88af to your computer and use it in GitHub Desktop.
Save spencershepard/70610283a92e33713cca010b2caf88af to your computer and use it in GitHub Desktop.
Init Terraform with AWS Backend (PowerShell)
$TF_STATE_BUCKET = "your-bucket-name"
$BUCKET_FOLDER = "terraform"
$TF_STATE_TABLE = "your-dynamodb-table"
$AWS_REGION = "your-aws-region"
aws s3api create-bucket --bucket $TF_STATE_BUCKET --region $AWS_REGION
if ($LASTEXITCODE -ne 0) { Write-Host "Bucket already exists" }
aws dynamodb create-table `
--table-name $TF_STATE_TABLE `
--attribute-definitions AttributeName=LockID,AttributeType=S `
--key-schema AttributeName=LockID,KeyType=HASH `
--billing-mode PAY_PER_REQUEST `
--region $AWS_REGION
if ($LASTEXITCODE -ne 0) { Write-Host "Table already exists" }
terraform init `
-backend-config="bucket=$TF_STATE_BUCKET" `
-backend-config="region=$AWS_REGION" `
-backend-config="dynamodb_table=$TF_STATE_TABLE" `
-backend-config="key=$BUCKET_FOLDER/terraform.tfstate" `
-backend-config="encrypt=true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment