User Tools

Site Tools


terraform:modules1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
terraform:modules1 [10/02/2025 15:35] andrewterraform:modules1 [12/02/2025 16:29] (current) andrew
Line 1: Line 1:
-====== Another Modules example ======+====== Basic Modules example ======
  
  
-To create an AWS parameter in a module.+To create an AWS parameter in a module, just the simplest example I could think ofThe access key and secret are exported as shell variables. This is not the best way to do this, but the object of the excercise is to play with modules not authentication. 
 + 
 +===== Code =====
  
  
Line 11: Line 13:
 ├── set_parameter ├── set_parameter
 │   ├── main.tf │   ├── main.tf
 +│   ├── output.tf
 │   └── variables.tf │   └── variables.tf
-── terraform.tfstate+── terraform.tfstate 
 +├── terraform.tfstate.backup 
 +└── variables.tf
 </code> </code>
 +
 +Variables are set in the ''variables.tf'' file:-
 +<code>
 +$ cat variables.tf
 +
 +variable "test1" {
 +  description = "test 1 string"
 +  type        = string
 +  default     = "variable1"
 +}
 +variable "test2" {
 +  description = "test 2 string"
 +  type        = string
 +  default     = "variable2"
 +}
 +</code>
 +
 +These are used in ''main.tf'':-
 +<code>
 +$ cat main.tf
 +provider "aws" {
 +  region = "eu-west-1"
 +  default_tags {
 +    tags = {
 +      BuiltBy = "Terraform"
 +    }
 +  }
 +}
 +
 +module "set_parameter" {
 +  source     = "./set_parameter"
 +  para_name  = var.test1
 +  para_value = var.test2
 +}
 +
 +output "Parameter_Name" {
 +  value = module.set_parameter.parameter_arn
 +}
 +</code>
 +
 +
 +In the set_parameter directory we have the files for the module. ''variables.tf'' defines the variables expected to be passed in to the module:-
 +<code>
 +set_parameter$ cat variables.tf
 +variable "para_name" {
 +  type = string
 +  description = "Parameter name"
 +}
 +
 +variable "para_value" {
 +  type = string
 +  description = "Parameter Value"
 +}
 +</code>
 +
 +
 +''main.tf'' does the work:-
 +<code>
 +set_parameter$ cat main.tf
 +resource "aws_ssm_parameter" "mypara" {
 +  name = var.para_name
 +  type = "String"
 +  value = var.para_value
 +}
 +</code>
 +
 +The values returned to the calling code are defined in ''output.tf'':-
 +<code>
 +set_parameter$ cat output.tf
 +# Outputs to export to the code calling this module
 +
 +output "parameter_arn" {
 +  value = aws_ssm_parameter.mypara.arn
 +}
 +</code>
 +
 +
 +===== Result =====
 +
 +This outputs the arn of the parameter created.
 +
 +<code bash>
 +$ terraform apply
 +
 +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
 +  + create
 +
 +Terraform will perform the following actions:
 +
 +  # module.set_parameter.aws_ssm_parameter.mypara will be created
 +  + resource "aws_ssm_parameter" "mypara" {
 +      + arn            = (known after apply)
 +      + data_type      = (known after apply)
 +      + id             = (known after apply)
 +      + insecure_value = (known after apply)
 +      + key_id         = (known after apply)
 +      + name           = "Myname"
 +      + tags_all       = {
 +          + "BuiltBy" = "Terraform"
 +        }
 +      + tier           = (known after apply)
 +      + type           = "String"
 +      + value          = (sensitive value)
 +      + version        = (known after apply)
 +    }
 +
 +Plan: 1 to add, 0 to change, 0 to destroy.
 +
 +Changes to Outputs:
 +  + Parameter_Name = (known after apply)
 +
 +
 +module.set_parameter.aws_ssm_parameter.mypara: Creating...
 +module.set_parameter.aws_ssm_parameter.mypara: Creation complete after 0s [id=Myname]
 +
 +Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
 +
 +Outputs:
 +
 +Parameter_Name = "arn:aws:ssm:eu-west-1:581230658448:parameter/variable1"
 +
 +$
 +</code>
 +
 +''terraform destroy'' will remove the parameter.
  
  
  
  
terraform/modules1.1739201713.txt.gz · Last modified: by andrew

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki