terraform:modules1
This is an old revision of the document!
Another Modules example
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.
user@ubuntu:~/projects/terraform$ tree . ├── main.tf ├── set_parameter │ ├── main.tf │ ├── output.tf │ └── variables.tf ├── terraform.tfstate ├── terraform.tfstate.backup └── variables.tf
Variables are set in the variables.tf
file:-
$ cat variables.tf variable "test1" { description = "test 1 string" type = string default = "variable1" } variable "test2" { description = "test 2 string" type = string default = "variable2" }
These are used in main.tf
:-
$ cat main.tf provider "aws" { region = "eu-west-1" default_tags { tags = { BuiltBy = "Terraform" } } } module "set_parameter" { source = "./set_parameter" para_name = "Myname" para_value = "Myvalue" } output "Parameter_Name" { value = module.set_parameter.parameter_arn }
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:-
set_parameter$ cat variables.tf variable "para_name" { type = string description = "Parameter name" } variable "para_value" { type = string description = "Parameter Value" }
main.tf
does the work:-
set_parameter$ cat main.tf resource "aws_ssm_parameter" "mypara" { name = var.para_name type = "String" value = var.para_value }
The values returned to the calling code are defined in output.tf
:-
set_parameter$ cat output.tf # Outputs to export to the code calling this module output "parameter_arn" { value = aws_ssm_parameter.mypara.arn }
terraform/modules1.1739207862.txt.gz · Last modified: by andrew