简体   繁体   中英

How could my Terraform deployment of a static website to an AWS S3 bucket be improved?

It is very basic at the moment.

connection.tf

provider "aws" {
    region = "eu-west-2"
}

main.tf

resource "aws_s3_bucket" "bucket" {
    bucket = "mybucket"
    acl = "public-read"
  
    provisioner "local-exec" {
        command = "aws s3 sync static/ s3://${aws_s3_bucket.bucket.bucket} --acl public-read --delete"
    }

    website {
        index_document = "index.html"
    }

}

I have a Github (CI/CD) Action that can rebuild the static/ and it's website contents when updates are pushed to the main branch.

So at the moment the Terraform files (I think) just provision the bucket and push the initial contents of static/

but is there anything else that can be done with Terraform? Or how can the inital deployment scripts be improved?

I'm new to Terraform but the static website is up and running on AWS S3.

I've researched online the best way to use Terraform (this is a requirement of the task) to deploy a static website to S3. Having the Github Action to (CI/CD) to update the website was the main suggestion. But not much was mentioned about how the Terraform aspect could be improved - optimised.

It seems very short and I expect there are many other configurations that should be included.

Some suggestions,

  • Every single names that can be used as variables, put them in a variables.tf file.
  • store the terraform state file in backend, preferably on an s3 bucket or terraform cloud (fairly new).
  • create a folder named 'scripts' and write some bash scripts (or powershell if on windows) to do the plan, apply, destroy etc. Why? Well after you proceed with terraform it's not just simply terraform apply , you may and most certainly will have to do more things, pass more parameters etc. So write some bash scripts and run them, let them do the heavy lifting. I'll give you a situation, if you run terraform destroy you can not delete a bucket with objects in it. But if you can't delete it, it will throw error on CI/CD pipeline. So what you can do is remove the resources from state file then perform destroy. Before applying you can attach again.

I also have a repo on static websites using terraform and AWS, feel free to check out

Best wishes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM