Some time spent using Terraform to create an EC2, S3 or a MongoDB cluster. Feel free to use this code as a starting point for your own projects !
Work to do here :
- Test even more OpenTofu tools
- Try to do a lightweight/cheaper version of the MongoDB cluster
################################################################################
################################################################################
Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
- Terraform >= 1.5
- You also need to define a few environment variables LOCALLY
Before anything else, you need to login to AWS and create a user with the following permissions:
AmazonEC2FullAccess
AmazonVPCFullAccess
AmazonS3FullAccess
AdministratorAccess
This service is available in the AWS Web-app under the IAM service :
AWS Web App > IAM > Users (on the left panel) > Add Users > (set the user name + check provide user access, then check "I want to create an IAM user" (define a password)) > Attach policies directly. Add following policies:
AmazonEC2FullAccess
AmazonVPCFullAccess
AmazonS3FullAccess
AdministratorAccess
... and then confirm.
Then go back to users, click on the user you just created, and click on the "Security credentials" tab.
Here you can find the Access key ID
and the Secret access key
that you need to define locally by creating a new Access key. Finally, you'll get these variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
These ones are used by Terraform to authenticate with AWS. Define them locally by running the following commands:
export AWS_ACCESS_KEY_ID=<your_access_key_id>
export AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
This operation is only needed once (if you're creating a brand new infrastructure).
You'll also need to define in variables.tf
the following variables (depending on your needs):
project_name
instance_type
Initiliaze the terraform configuration
terraform init
Generate and show an execution plan. This is mainly used to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.
terraform plan
Apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan
execution plan.
terraform apply
If you want to get back some information about the infrastructure you just created, you can run the following command:
terraform show
... or, if you just need outputs:
terraform output -json