cours
cours
1. Introduction
2. Concepts of Terraform
3. Providers
4. Good practices
5. Your turn...
1
Introduction
Cloud computing - what you have seen so far
2
What you have seen in the DevOps course
3
DevOps (SRE) skills
1. Concepts of development
2. Operating systems
3. Networking and security
4. Containers
5. Automated CI/CD
6. Cloud providers
Colors 7. Containers orchestration
• already known 8. Monitoring
• DevOps course 9. Infrastructure as Code
• This year 10. Scripting
• What is not studied in FIL 11. Version control
4
Terraform
5
Advantages of Terraform
6
Why Terraform?
I can do that through the graphical interfaces of Cloud providers! Yes but...
• Long and error-prone manual procedures.
• Difficult and error-prone when collaborating.
• Not scalable.
I can do that with Cloud providers CLIs and scripts! Yes but...
• You have to know as many CLIs as the number of Cloud providers you are using.
• A script is less specialized and structured than IaC, more difficult to write/read and
maintain.
• You have to manually handle the state of your infrastructure which is difficult and
error prone.
7
Organization
Evaluated skills
• CG2 project (by 2)
8
Concepts of Terraform
Automatic ordering
9
Automatic ordering
9
Automatic ordering
We can create multiple versions of the same replicated infrastructure (e.g. dev, prod).
9
Basic Architecture
Resources
• A resource can represent anything. e.g. VM, docker image, virtual network, ip, user,
account, role, etc.
• Providers furnish an API that lists
1. Available resource types.
2. For each of them their parameters.
10
Basic Architecture
Resources
• A resource can represent anything. e.g. VM, docker image, virtual network, ip, user,
account, role, etc.
• Providers furnish an API that lists
1. Available resource types.
2. For each of them their parameters.
Terraform Core
• Configuration : every .tf files ⇒ resources declarations.
• The current directory constitutes the root module.
• State file : contains the current state of resources under Terraform’s management.
• Upon each CLI call, the state file is refreshed with the actual resources.
10
Basic Architecture
Resources
• A resource can represent anything. e.g. VM, docker image, virtual network, ip, user,
account, role, etc.
• Providers furnish an API that lists
1. Available resource types.
2. For each of them their parameters.
Terraform Core
• Configuration : every .tf files ⇒ resources declarations.
• The current directory constitutes the root module.
• State file : contains the current state of resources under Terraform’s management.
• Upon each CLI call, the state file is refreshed with the actual resources.
Terraform detects changes in the configuration and plan API calls accordingly.
10
Commands for different stages
terraform refresh
Updates the state file by querying the provider.
11
Commands for different stages
terraform refresh
Updates the state file by querying the provider.
terraform plan
Produce an execution plan with details on what to add/delete/change by comparing the
‘.tf‘ configurations and the state file.
11
Commands for different stages
terraform refresh
Updates the state file by querying the provider.
terraform plan
Produce an execution plan with details on what to add/delete/change by comparing the
‘.tf‘ configurations and the state file.
11
Commands for different stages
terraform refresh
Updates the state file by querying the provider.
terraform plan
Produce an execution plan with details on what to add/delete/change by comparing the
‘.tf‘ configurations and the state file.
terraform destroy
Calls the provider to deletes managed resources.
11
HCL Language Syntax (1): Attributes
• credentials = file("./creds.json")
• labels = { app = "redis" }
• image = docker_image.myimage.name
• etc.
12
HCL Language Syntax (1): Attributes
• credentials = file("./creds.json")
• labels = { app = "redis" }
• image = docker_image.myimage.name
• etc.
12
HCL Language Syntax (2): Blocks
Blocks
e.g. resource "docker_image" "redis" { ... }
• Have a key identifier: here resource. It has a meaning in the context where it is
defined
• String identifiers attached: here docker_image and redis.
• They can represent a type or name identifier in order to refer to them in another part
of the .tf configuration.
13
HCL Language Syntax (2): Blocks
Blocks
e.g. resource "docker_image" "redis" { ... }
• Have a key identifier: here resource. It has a meaning in the context where it is
defined
• String identifiers attached: here docker_image and redis.
• They can represent a type or name identifier in order to refer to them in another part
of the .tf configuration.
Block can have embedded blocks that, again, have meaning only in the context of the
current block. e.g. docker_image block can embed a build block.
13
HCL Language Syntax (2): Blocks
Blocks
e.g. resource "docker_image" "redis" { ... }
• Have a key identifier: here resource. It has a meaning in the context where it is
defined
• String identifiers attached: here docker_image and redis.
• They can represent a type or name identifier in order to refer to them in another part
of the .tf configuration.
Block can have embedded blocks that, again, have meaning only in the context of the
current block. e.g. docker_image block can embed a build block.
Multiple embedded block with the same keyword are sometimes allowed. It usually
results in a list of objects.
13
Kinds of top-level blocks
Terraform has concepts for each kind of block that can be declared at the top-level.
resource, data, provider
The references for those blocks are found in the provider’s documentation at
https://registry.terraform.io/providers/.
14
Kinds of top-level blocks
Terraform has concepts for each kind of block that can be declared at the top-level.
resource, data, provider
The references for those blocks are found in the provider’s documentation at
https://registry.terraform.io/providers/.
14
Kinds of top-level blocks
Terraform has concepts for each kind of block that can be declared at the top-level.
resource, data, provider
The references for those blocks are found in the provider’s documentation at
https://registry.terraform.io/providers/.
There are a few other top-level blocks, e.g. locals, module, etc.
14
Variables
User variables
The DevOps can declare three kinds of user variables: Input, Output, and Local.
They are declared in the variable, output, and locals blocks respectively.
15
Variables
User variables
The DevOps can declare three kinds of user variables: Input, Output, and Local.
They are declared in the variable, output, and locals blocks respectively.
15
Providers
Provider registry
16
Good practices
Good practices
17
Your turn...
18