In this tutorial you will discover an example of Instance automation using Python and Terraform:
- Automatically shut down / start instances
- You have an account and are logged into the Scaleway console
- You have generated an API key
- You have Python installed on your machine
- You have Terraform installed on your machine
- You are familiar with Instance API that can be found in the developers documentation.
In this tutorial, we will simulate a project with a production environment that will be running all the time and a development environment that will be turn off on week-ends to save costs.
-
This folder stores your configuration as explained in the terraform documentation.
-
This folder contains 5 files to configure your infrastructure: a. 'main.tf': will contain the main set of configurations for your project. Here, it will be our instance. b. 'provider.tf': Terraform relies on plugins called “providers” to interact with remote systems. c. 'variables.tf': will contain the variable definitions for your project. Since all Terraform values must be defined, any variables that are not given a default value will become required arguments. d. 'terraform.tfvars': allows you to set the actual value of the variables.
-
Create the following folder: a. 'function': to store your function code. b. 'files': (generated in
main.tf
when creating function's zip file) to temporarily store your zip function code.
Now that everything is set up, deploy everything using Terraform
- Add your Scaleway credentials to your environment variables
export SCW_ACCESS_KEY="<your-secret-key>"
export SCW_SECRET_KEY="<your-access-key>"
- Initialize Terraform:
terraform init
- Let terraform verify your configuration:
terraform plan
- Deploy your infrastructure:
terraform apply
- In order to remove your infrastructure resources, you can use:
terraform destroy