terraform:modules1
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
terraform:modules1 [10/02/2025 15:31] – created andrew | terraform:modules1 [12/02/2025 16:29] (current) – andrew | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | To create an AWS parameter in a module. | + | To create an AWS parameter in a module, just the simplest example I could think of. The 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 ===== | ||
+ | |||
+ | |||
+ | <code bash> | ||
+ | user@ubuntu: | ||
+ | . | ||
+ | ├── main.tf | ||
+ | ├── set_parameter | ||
+ | │ ├── main.tf | ||
+ | │ ├── output.tf | ||
+ | │ └── variables.tf | ||
+ | ├── terraform.tfstate | ||
+ | ├── terraform.tfstate.backup | ||
+ | └── variables.tf | ||
+ | </ | ||
+ | |||
+ | Variables are set in the '' | ||
+ | < | ||
+ | $ cat variables.tf | ||
+ | |||
+ | variable " | ||
+ | description = "test 1 string" | ||
+ | type = string | ||
+ | default | ||
+ | } | ||
+ | variable " | ||
+ | description = "test 2 string" | ||
+ | type = string | ||
+ | default | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | These are used in '' | ||
+ | < | ||
+ | $ cat main.tf | ||
+ | provider " | ||
+ | region = " | ||
+ | default_tags { | ||
+ | tags = { | ||
+ | BuiltBy = " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | module " | ||
+ | source | ||
+ | para_name | ||
+ | para_value = var.test2 | ||
+ | } | ||
+ | |||
+ | output " | ||
+ | value = module.set_parameter.parameter_arn | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | In the set_parameter directory we have the files for the module. '' | ||
+ | < | ||
+ | set_parameter$ cat variables.tf | ||
+ | variable " | ||
+ | type = string | ||
+ | description = " | ||
+ | } | ||
+ | |||
+ | variable " | ||
+ | type = string | ||
+ | description = " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | '' | ||
+ | < | ||
+ | set_parameter$ cat main.tf | ||
+ | resource " | ||
+ | name = var.para_name | ||
+ | type = " | ||
+ | value = var.para_value | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The values returned to the calling code are defined in '' | ||
+ | < | ||
+ | set_parameter$ cat output.tf | ||
+ | # Outputs to export to the code calling this module | ||
+ | |||
+ | output " | ||
+ | value = aws_ssm_parameter.mypara.arn | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 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 " | ||
+ | + arn = (known after apply) | ||
+ | + data_type | ||
+ | + id = (known after apply) | ||
+ | + insecure_value = (known after apply) | ||
+ | + key_id | ||
+ | + name = " | ||
+ | + tags_all | ||
+ | + " | ||
+ | } | ||
+ | + tier = (known after apply) | ||
+ | + type = " | ||
+ | + value = (sensitive value) | ||
+ | + version | ||
+ | } | ||
+ | |||
+ | 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: | ||
+ | module.set_parameter.aws_ssm_parameter.mypara: | ||
+ | |||
+ | Apply complete! Resources: 1 added, 0 changed, 0 destroyed. | ||
+ | |||
+ | Outputs: | ||
+ | |||
+ | Parameter_Name = " | ||
+ | |||
+ | $ | ||
+ | </ | ||
+ | |||
+ | '' | ||
terraform/modules1.1739201507.txt.gz · Last modified: by andrew