Terraform string to map

15.04.2021 By Yogore

You now have enough Terraform knowledge to create useful configurations, but we're still hard-coding access keys, AMIs, etc. To become truly shareable and version controlled, we need to parameterize the configurations.

This page introduces input variables as a way to do this. Let's first extract our region into a variable. Create another file variables. Note : The file can be named anything, since Terraform loads all files ending in. This defines the region variable within your Terraform configuration. There is a default value which will be used if it is not set elsewhere. If no default is set in this block, the value is required and must be set using one of the methods shown later in this guide.

This uses the variable named regionprefixed with var. It tells Terraform that you're accessing a variable and that the value of the region variable should be used here.

It configures the AWS provider with the given variable. There are multiple ways to assign variables. The order below is also the order in which variable values are chosen.

You can set variables directly on the command-line with the -var flag. Any command in Terraform that inspects the configuration accepts this flag, such as applyplanand refresh. Once again, setting variables this way will not save them, and they'll have to be entered repeatedly as commands are executed. To persist variable values, create a file and assign variables within this file. Create a file named terraform. Terraform automatically loads all files in the current directory with the exact name of terraform.

If the file is named something else, you can use the -var-file flag to specify a file name. These files use the same syntax as Terraform configuration files HCL.

terraform string to map

We don't recommend saving usernames and passwords to version control. You can create a local file with a name like secret. You can use multiple -var-file arguments in a single command, with some checked in to version control and others not checked in. Tip: This is one way to provision infrastructure in a staging environment or a production environment using the same Terraform configuration.

Note : Environment variables can only populate string-type variables. List and map type variables must be populated via one of the other mechanisms. If you execute terraform apply with any variable unspecified, Terraform will ask you to input the values interactively. These values are not saved, but this provides a convenient workflow when getting started with Terraform.

UI input is not recommended for everyday use of Terraform. Note : In Terraform versions 0. List and map variables must be populated via one of the other mechanisms. Terraform 0. If no value is assigned to a variable via any of these methods and the variable has a default key in its declaration, that value will be used for the variable.

Strings and numbers are the most commonly used variables, but lists arrays and maps hashtables or dictionaries can also be used. We've replaced our sensitive strings with variables, but we are still hard-coding AMIs.

Unfortunately, AMIs are specific to the geographical region in use. One option is to ask the user to input the proper AMI for the region, but Terraform can do better than that with a map.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Subscribe to RSS

I tried few things here - e. So my question would be - does anyone has working example where he did translated string or list into a map? Learn more. Convert string or array to map in Terraform? Ask Question. Asked 1 year, 2 months ago. Active 1 year, 2 months ago. Viewed 1k times. Terraform v0. CommonRules,element keys var.

Thanks in advance. Igor Igor 3 3 silver badges 15 15 bronze badges. Have you tried using the external data source as defined in this question.

Active Oldest Votes. Sign up or log in Sign up using Google. Sign up using Facebook.

terraform string to map

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. The Overflow How many jobs can be done at home? Featured on Meta. Community and Moderator guidelines for escalating issues via new responseā€¦. Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow.

Triage needs to be fixed urgently, and users need to be notified uponā€¦. Dark Mode Beta - help us root out low-contrast and un-converted bits.

Terraform Repository Best Practices, Part 2

Linked GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. Here's what I did for multi-environment in one variables. I just verified that the example given in the original comment on this issue works in Terraform v0.

In addition, Terraform v0. Although there is still a remaining small issue to fix over inthis feature request is now complete. Thanks for your patience, everyone! FYI: Terraform v0. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. New issue. Jump to bottom. Labels config enhancement. Milestone v0. Copy link Quote reply. This comment has been minimized. Sign in to view. Contributor Author.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Have a question about this project?

terraform string to map

Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. Currently Terraform and HCL can do iteration using count and then the lookup and element functions. These work for simple cases, but they fail if there's a need to do a more complex iterations and nested definitions. There has been similar suggestions like and I would love.

It does, however, break the declarative nature of things and not all 'creative' solutions are able to function purely declaratively. I've chosen to go with maps, and utilize lengthelementkeysand values. In the above link you can see that I dynamically create the subnets based on the information held in the var. I have a number of things keyed off this, however to the user, all they really need to do is create a map with suitable key-value pairs that aws consumes. I know this isn't really a solution as it were, but as close to a work-around as possible.

Thanks mengesb for the comments. I tend to think that using loops and iterations in this kind of setup doesn't break the declarative nature how Terraform currently works. So for example a code which would iterate over a list of two entries and declare two resources, the produced JSON would look like there would be no loop but just two declared resources. Actually I think that the current limitations forces Terraform users to workaround with a really complex way of using countelement and other functions, which could be made much more human readable and easier to debug with proper control structures instead.

I would absolutely love to see loops and conditionals in HCL. It would improve Terraform significantly. I think even our Hashicorp friends are craving for this when you see the effort they had to go to so they can iterate through items in their best-practices repo.

Just want to clarify that you don't actually have to do what was done above in the best-practices repo. I'm a lot happier after I gave up on trying to do everything from within terraform. This is not a ding against terraform. All custom external DSLs have this problem. It would have been different if terraform was a library like Chef that allowed constructing the template structure with actual code but alas it is not.

This actually opens up a possibility for someone to take up that torch. Build a real DSL in a real language that can generate the proper terraform template. Just as a heads up, I had the same problem myself and created a small wrapper that you can use to use Jinja2 templates in terraform. This script also makes all your variables available to Jinja. Just in case anyone is looking for a ready to use script :. The sample map looks like following:. Please have a look into the code below:.

Thought about something like this before but did not even try since I was burned so many times already by Terrafrom when I tried to do some clever workaround.

I'm amazed this is actually working. Thanks for sharing. It would be nice if there were a way to take a basic map iterate over that to create a decent map that could used for ASGs.

The concept is simple, instead of enhancing DSL to provide basic programming constructs if, else, for, while, etc. Please refer to my article to see an example usage: Add loops, conditions and logic in Terraform or CloudFormation using JavaScript. It appears that all you're doing is templating the DSL itself.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time.

Input Variables

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I tried few things here - e. So my question would be - does anyone has working example where he did translated string or list into a map? Learn more. Convert string or array to map in Terraform? Ask Question. Asked 1 year, 2 months ago. Active 1 year, 2 months ago. Viewed 1k times. Terraform v0. CommonRules,element keys var.

Thanks in advance. Igor Igor 3 3 silver badges 15 15 bronze badges. Have you tried using the external data source as defined in this question. Active Oldest Votes. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Podcast Cryptocurrency-Based Life Forms. Q2 Community Roadmap. Featured on Meta. Community and Moderator guidelines for escalating issues via new responseā€¦. Feedback on Q2 Community Roadmap. Triage needs to be fixed urgently, and users need to be notified uponā€¦. Dark Mode Beta - help us root out low-contrast and un-converted bits.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Upgraded to terraform v0. Anyway, fixed most of the things but the variable interpolation of type map still not working. But then I cannot figure out how to use that for SG rules in the module. Anyone knows what am I missing or how to fix that?

Really stuck since Friday evening. Any pointed, example-code would be highly appreciated. Learn more. Variable interpolation for type map Ask Question. Asked 9 months ago.

Active 9 months ago. Viewed times. Rob MacUsers MacUsers 1, 2 2 gold badges 27 27 silver badges 40 40 bronze badges. Already tried that. Unless I'm terribly missing something here.

Active Oldest Votes. Thanks rflume! On a related matter, instead of using [lan][ovp] staticly, is there a way to get all the keys dynamically? Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Cryptocurrency-Based Life Forms. Q2 Community Roadmap.

Subscribe to RSS

Featured on Meta. Community and Moderator guidelines for escalating issues via new responseā€¦. Feedback on Q2 Community Roadmap.

Triage needs to be fixed urgently, and users need to be notified uponā€¦. Dark Mode Beta - help us root out low-contrast and un-converted bits.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. Currently, I can convert a string to a list, using the split method, but is there a better way?

And how to convert string to map? Hi apparentlymart I have a stringlike this :. Hi LanceNero. At the moment there is no built-in way to decode a JSON string. There is some discussion about this inincluding information about our plans here. In the mean time, it is necessary to pass data into Terraform some other way, such as generating a.

Hi apparentlymart Ok, got it Thanks your reply. If so, you'll need to find the Windows equivalent This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. New issue. Jump to bottom. Copy link Quote reply. Hi there, Terraform Version Terraform v0. LanceNero changed the title [Question] How to use terraform to convert string to list or map [Question] How to convert string to list or map Dec 7, This comment has been minimized.

terraform string to map

Sign in to view. LanceNero closed this Dec 8, I run on windows within poweshell. There I have echo command. Sign up for free to subscribe to this conversation on GitHub. Already have an account?

Sign in. Linked pull requests. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Thanks for any help!