Recently I was playing with ChatGPT and its ability to generate code. Frankly, I am amazed. While I see some people saying coders will become obsolete, I firmly stand that it is actually quite the opposite. ChatGPT will not take our jobs but rather empower us to do things faster. I really see it as an empowerment tool rather than a competition.
Anyway, I’ve wrote following command: write a terraform script that will provision cloudfront distribution and use s3 as a source for a static website.
The response
Here is an example of a Terraform script that provisions an Amazon CloudFront distribution and uses an S3 bucket as a source for a static website:
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-static-website"
acl = "public-read"
}
resource "aws_s3_bucket_policy" "my_policy" {
bucket = aws_s3_bucket.my_bucket.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-static-website/*"
}
]
}
EOF
}
resource "aws_cloudfront_distribution" "my_distribution" {
origin {
domain_name = aws_s3_bucket.my_bucket.bucket_domain_name
origin_id = "my_origin"
}
default_cache_behavior {
target_origin_id = "my_origin"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
viewer_certificate {
cloudfront_default_certificate = true
}
enabled = true
}
This script creates an S3 bucket with the name “my-static-website” and a public-read ACL, as well as a bucket policy that allows public read access to objects in the bucket. It then creates a CloudFront distribution with the S3 bucket as the origin, using the bucket’s domain name as the origin domain name. The distribution is configured to redirect all requests to HTTPS, to cache objects for up to one day, and to use the default CloudFront certificate for SSL/TLS.
To use this script, you will need to have the AWS provider configured in your Terraform configuration. You can do this by specifying your AWS access key and secret key as environment variables or by using an AWS profile. You can find more information on how to do this in the AWS provider documentation.
Note that this is just one example of how to set up a CloudFront distribution with an S3 bucket as the source for a static website. There are many other configuration options available that you can use to customize the behavior of your distribution. You can find more information on these options in the AWS documentation.
As you can see, it is a great tool for code generation as it saves time going through the documentation and writing the code manually. While this code might not always be perfect for some scripting stuff, I find it quite useful. I was not yet testing with more complex tasks, but I will :)