Professional Documents
Culture Documents
tform s3 cld
tform s3 cld
If you don’t know how to create an access key and secret key, don’t worry. We have the next section for
it.
2. Select IAM
Please keep in mind to select Programmatic access in Access type to get Access Key ID and Secret Key.
5. Attach policy
Select Next: Tags button displayed below and then Add Tags (optional).
6. 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
Then without a doubt, get in touch with us to work with like-minded and efficient DevOps
In this section, we will first discuss the S3 bucket and then the main Terraform configuration file. We will
Create a module that will have a basic S3 file configuration. For that, create one folder named “S3,” we
2. Define bucket
bucket.tf
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
Bucket and ACL are the argument types for our resource. We can have different arguments
Either we can provide value directly or use the var.tf file to declare the value of an argument.
3. Define variables
var.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.
main.tf
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
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
In argument, we will provide a source to the S3 module and bucket name, as we haven’t defined
While writing bucket name, please keep in mind that its name is unique in the region, and it
Now we will define variable.tf, where we will enter our access key, secret key, and region.
variable.tf
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.
If you haven’t downloaded terraform then visit the Terraform official document for downloading
You can check the version of terraform installed in your machine using terraform -v command.
1. terraform init
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.
3. terraform apply
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.
terraform destroy
or
After applying terraform destroy, you will see something like this-