Import blocks in json format

Hi!

We to use json instead of hcl because we generate our terraform code programmatically.

I do experiment with importing resources, so there is imports in hcl:

import {
  to = aws_security_group_rule.allow_tls
  id = "${aws_security_group.allow_tls.id}_ingress_tcp_443_443_0.0.0.0/0_::/0"
}

import {
  to = aws_security_group_rule.allow_http
  id = "${aws_security_group.allow_tls.id}_ingress_tcp_80_80_0.0.0.0/0_::/0"
}

I’m trying to convert this to json:

    "import": [
        {
            "to": "${aws_security_group_rule.allow_tls}",
            "id": "${aws_security_group.allow_tls.id}_ingress_tcp_443_443_0.0.0.0/0_::/0"
        },
        {
            "to": "${aws_security_group_rule.allow_http}",
            "id": "${aws_security_group.allow_tls.id}_ingress_tcp_80_80_0.0.0.0/0_::/0"
        }
    ]

This gives the following errors:

β”‚ Error: Invalid character
β”‚ 
β”‚   on main.tf.json line 52, in import[0]:
β”‚   52:             "to": "${aws_security_group_rule.allow_tls}",
β”‚ 
β”‚ This character is not used within the language.
β•΅
β•·
β”‚ Error: Invalid expression
β”‚ 
β”‚   on main.tf.json line 52, in import[0]:
β”‚   52:             "to": "${aws_security_group_rule.allow_tls}",
β”‚ 
β”‚ Expected the start of an expression, but found an invalid expression token.

How to properly represent import blocks in json?

Hi @johndoe1,

The to argument is a static resource address, you are not trying to interpolate the value of aws_security_group_rule.allow_tls so you don’t want the ${...} interpolation syntax.

{
  "to": "aws_security_group_rule.allow_tls",
  "id": "${aws_security_group.allow_tls.id}_ingress_tcp_443_443_0.0.0.0/0_::/0"
},
1 Like

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.