How To Create S3 Bucket Using Terraform
Creating an S3 Bucket Module in Terraform
Before I get started, you can go find my code in my repo at this link.
This bucket module is going to be made of a few different files.
- Main.tf — for configuration
- Variables.tf — for variables
- Outputs.tf — for outputs
First we will take a look at the main.tf configuration.
Main.tf File
resource "aws_s3_bucket" "b" {
bucket_prefix = var.bucket_prefix
acl = var.acl versioning {
enabled = var.versioning
} logging {
target_bucket = var.target_bucket
target_prefix = var.target_prefix
} server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_master_key_id
sse_algorithm = var.sse_algorithm
}
}
} tags = var.tags
}
We are going to do a couple things here that I want to note. First, we will be setting variables for every argument so that we can create some defaults. Second, we are choosing to use the bucket_prefix argument rather than the bucket argument. That way we don't accidentally try to create a bucket with the same name as one that already exists in the global namespace.
When we use bucket_prefix it would be best to name the bucket something like my-bucket- that way the string added to the end of the bucket name comes after the dash.
Variables.tf File
variable "bucket_prefix" {
type = string
description = "(required since we are not using 'bucket') Creates a unique bucket name beginning with the specified prefix. Conflicts with bucket."
default = ""
} variable "acl" {
type = string
description = "(Optional) The canned ACL to apply. Defaults to private. Conflicts with grant."
default = "private"
} variable "versioning" {
type = bool
description = "(Optional) A state of versioning."
default = true
} variable "target_bucket" {
type = string
description = "(Required) The name of the bucket that will receive the log objects."
default = ""
} variable "target_prefix" {
type = string
description = "(Optional) To specify a key prefix for log objects."
default = "log/"
} variable "kms_master_key_id" {
type = string
description = "(optional) The AWS KMS master key ID used for the SSE-KMS encryption. This can only be used when you set the value of sse_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse_algorithm is aws:kms."
default = "aws/s3"
} variable "sse_algorithm" {
type = string
description = "(required) The server-side encryption algorithm to use. Valid values are AES256 and aws:kms"
default = "aws:kms"
} variable "tags" {
type = map
description = "(Optional) A mapping of tags to assign to the bucket."
default = {
environment = "prod"
terraform = "true"
}
}
Next we add in the contents for the variables.tf file. We create a variable for every var.example variable that we set in our main.tf file and create defaults for anything we can.
Next, let's take a look at outputs.
Outputs.tf File
output "s3_bucket_id" {
value = aws_s3_bucket.s3_bucket.id
} output "s3_bucket_arn" {
value = aws_s3_bucket.s3_bucket.arn
} output "s3_bucket_domain_name" {
value = aws_s3_bucket.s3_bucket.bucket_domain_name
} output "s3_hosted_zone_id" {
value = aws_s3_bucket.s3_bucket.hosted_zone_id
} output "s3_bucket_region" {
value = aws_s3_bucket.s3_bucket.region
}
Here we just include outputs that we might be interested in seeing. The configuration in this file is not required to make our module work.
Example Usage
module s3_bucket {
source = "github.com/jakeasarus/terraform/s3_bucket"
bucket_prefix = "this-is-only-a-test-bucket-delete-me-123"
target_bucket = "this-is-the-target-bucket"
}
This is an example of the usage. Source tells Terraform where to go to get the module information, mine is stored at that url. Next, bucket_prefix is the prefix we discussed using earlier. Last, target_bucket is the target bucket we want to use to store our logging.
If you want to see more information about this module go checkout the README.md in my repo.
I hope you enjoyed this and found it helpful! If you are interested in learning more about Terraform I have a Free Terraform Course for getting started and a course to help you study for your HashiCorp Certified: Terraform Associate.
I also highly suggest checking out Terraform Up & Running by Yevgeniy Brikman.
Happy learning!
👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.
Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇
How To Create S3 Bucket Using Terraform
Source: https://faun.pub/creating-an-s3-bucket-module-in-terraform-c89b77f2b1ae
Posted by: dixonaname1987.blogspot.com
0 Response to "How To Create S3 Bucket Using Terraform"
Post a Comment