Prerequisites
An AWS IAM role with S3 permissions
Access key ID & Secret Access Key of that account.
If you don’t know how to create an access key and secret key, don’t worry. We have the next section for it.
How to create an Access Key and Secret Key?
1. Login to your AWS Account
2. Select IAM
In Management Console, search for IAM.
3. On the left side of the panel, select User*.*
4. Select Add Users and enter details.
Please keep in mind to select Programmatic access in Access type to get Access Key ID and Secret Key.
5. Attach policy
After entering the details, attach a policy for S3 as shown below.
Select Next: Tags button displayed below and then Add Tags (optional).
6. Create User.
Review your user policy and then Create User.
After Successful IAM user creation, you will see a message (as shown below) with your Access key and Secret key.
Note- Store this Access Key and Secret Key to your Local Computer as AWS does not allow you to retrieve secret keys after its creation.
Now, moving towards how to create an S3 bucket using Terraform.
Looking for an expert to deploy your project or for DevOps consultation?
Then without a doubt, get in touch with us to work with like-minded and efficient DevOps programmers. Contact us to hire DevOps developer.
Steps to create an S3 bucket using Terraform
In this section, we will first discuss the S3 bucket and then the main Terraform configuration file. We will also cover the AWS S3 object bucket in terraform.
1. Create S3 bucket module
Create a module that will have a basic S3 file configuration. For that, create one folder named “S3,” we will have two files: bucket.tf and var.tf.
2. Define bucket
Open bucket.tf and define bucket in that.
resource "aws_s3_bucket" "demos3" {
bucket = "${var.bucket_name}"
acl = "${var.acl_value}"
}
Explanation
We have a block with the key name “resource” with resource type “aws_s3_bucket”– which we want to create. It has a fixed value, and it depends on the provider. Here we have an AWS S3 resource where AWS is our provider and S3 is our resource. “Demos3” is the resource name that the user provides.
Bucket and ACL are the argument types for our resource. We can have different arguments according to our needs and their corresponding values.
Either we can provide value directly or use the var.tf file to declare the value of an argument.
3. Define variables
In var.tf, we will define variables for the bucket.tf
variable "bucket_name" {}
variable "acl_value" {
default = "private"
}
Explanation
- As mentioned above, var.tf is used to declare values of variables. We can either provide a default value to be used when needed or ask for value during execution.
4. Add Configuration
After successfully creating the S3 folder, create a file named main.tf for keeping configuration in our working directory.
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.region}"
}
module "s3" {
source = "<path-to-S3-folder>"
#bucket name should be unique
bucket_name = "<Bucket-name>"
}
Explanation
It contains the main set of the module’s configurations.
Here we provide details of our provider (AWS) and access key, secret key, etc.
Since we are creating S3 using terraform modules, we need to add an S3 module to create an S3 bucket. For this, we will use the keyword “module” and the name of the module (folder) which we have created earlier.
In argument, we will provide a source to the S3 module and bucket name, as we haven’t defined bucket name in var.tf.
While writing bucket name, please keep in mind that its name is unique in the region, and it does not contain “_” or Uppercase letters.
5. Add Access key, Secret key, and Region.
Now we will define variable.tf, where we will enter our access key, secret key, and region.
variable "aws_access_key" {
default = “<your_access_key>”
}
variable "aws_secret_key" {
default = “<your_secret_key>”
}
variable "region" {
default = "region"
}
Explanation
- Access key, Secret key, and Region will be defined here.
We are done with creating the S3 bucket; now it’s time to set up Terraform.
Run Terraform script in your system.
If you haven’t downloaded terraform then visit the Terraform official document for downloading Terraform in your system.
You can check the version of terraform installed in your machine using terraform -v command.
Run the following commands to run Terraform script in your system.
1. terraform init
It is used to initialize the working directory.
It will install the required plugins for our code, e.g., AWS S3.
You will see something like after running terraform init successfully-
2. terraform plan
We will use this command for script verification. It will show if there is an error in our configuration.
The output of terraform plan looks like this if it runs successfully-
3. terraform apply
Use terraform apply to create your S3 bucket.
It will ask you for confirmation before execution; enter yes for confirmation.
Use terraform apply -auto-approve if you want to execute it without asking for confirmation.
After successful execution, it will display the following message-
You can verify your bucket in S3 services in your AWS Account.
Your Bucket will be created in the desired region.
To destroy the S3 bucket, use this command-
terraform destroy
or
terraform destroy -auto-approve // if you don’t want to approve manually
After applying terraform destroy, you will see something like this-
So, this was about how to create an S3 bucket using Terraform.
I am Sunil kumar, Please do follow me here and support #devOps #trainwithshubham #github #devopscommunity #devops #cloud #devoparticles #trainwithshubham
Connect with me over linkedin : linkedin.com/in/sunilkumar2807