Here is a simple practice exercise to help you get started with Terraform:
- Create an S3 bucket: Create a new directory for your Terraform project and create a new file called
main.tf
. Add the following code to the file to create an S3 bucket in theus-east-1
region:
pythonCopy codeprovider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "example" {
bucket = "my-example-bucket"
acl = "private"
}
- Initialize and apply changes: Run the following commands to initialize the project and apply the changes:
csharpCopy codeterraform init
terraform apply
This will download the necessary plugins and initialize the state. Once you confirm the changes, Terraform will create the S3 bucket.
- Add an object to the bucket: Create a new file in the same directory called
example.txt
. Add some content to the file, and then add the following code to themain.tf
file to upload the file to the S3 bucket:
pythonCopy coderesource "aws_s3_bucket_object" "example" {
bucket = aws_s3_bucket.example.id
key = "example.txt"
source = "example.txt"
}
- Initialize and apply changes: Run the following commands to initialize the project and apply the changes:
csharpCopy codeterraform init
terraform apply
This will upload the example.txt
file to the S3 bucket.
- Clean up resources: To clean up the resources created by Terraform, run the following command:
Copy codeterraform destroy
This will delete the S3 bucket and the example.txt
file.
This is just a simple example, but Terraform can do much more, including creating more complex resources, managing state, and handling dependencies. The official Terraform documentation is a great resource for learning more about the tool and its capabilities.