aws-cloudformation / cfn-language-discussion

Language discussions for CloudFormation template language
https://aws.amazon.com/cloudformation/
Apache License 2.0
143 stars 13 forks source link

Feature request: a new Fn::Get intrinsic function to get a value by key from (JSON) data #129

Open mrinaudo-aws opened 1 year ago

mrinaudo-aws commented 1 year ago

Community Note

Tell us about your request

What do you want us to build? An intrinsic function to retrieve a value from JSON data, given a specified key. For example, given the following example output that the AwsCommunity::Resource::Lookup resource type would return for a given resource type target of a lookup using !GetAtt ResourceLookup.ResourceProperties (content has been omitted, and formatted using a two-space, newline indentation):

{
  "VpcId": "vpc-[OMITTED]",
  "InstanceTenancy": "default",
  "CidrBlockAssociations": [
    "vpc-cidr-assoc-[OMITTED]"
  ],
  "CidrBlock": "[OMITTED]",
  "DefaultNetworkAcl": "acl-[OMITTED]",
  "EnableDnsSupport": true,
  "Ipv6CidrBlocks": [],
  "DefaultSecurityGroup": "sg-[OMITTED]",
  "EnableDnsHostnames": true,
  "Tags": [
    {
      "Key": "Name",
      "Value": "Test"
    }
  ]
}

I would like to have a new intrinsic function, e.g., Fn::Get, to consume a value given its key. The value can be a string, a boolean, a list (whereas I could plug in Fn::Select for example to get a value given its index), a map, et cetera.

Tell us about the problem you are trying to solve. What are you trying to do, and why is it hard?

I think that having a native way (as opposed to building/maintaining one) for the above use case or similar ones, is desirable.

Are you currently working around this issue?

Thinking of experimenting with a custom logic to fetch value(s) I'd need - for example, a custom resource/resource type.

What is the expected behavior with this new feature

I want to be able to reference an input block (in the example above, the resource properties), and a path to navigate the property tree. An initial, simplified example could just reference to the key name (as in the example below), or use a JMESPath expression to retrieve nested structures:

Resources:
  MySubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: "us-east-1a"
      VpcId:
        Fn::Get:
          - !GetAtt ResourceLookup.ResourceProperties
          - VpcId

Additional context

-

Attachments

-

josb commented 1 year ago

It would be excellent if this also worked on String parameters, because it would provide for structured data as parameter values. While it would be preferable for CloudFormation to understand structured parameters natively (see e.g. https://github.com/aws-cloudformation/cfn-language-discussion/issues/46), this would be immediately helpful to group information together in a single parameter.

Zuhairahmed commented 4 months ago

just FYI, this is super helpful for us at MongoDB publisher side if CFN team could adopt this GitHub issue. this would help several of our joint-customers. happy to share more details if you need! cc: @mrinaudo-aws @lantoli

mrinaudo-aws commented 4 months ago

Not an intrinsic function, but I built this example custom resource that returns a value from JSON data and a search argument you both specify. Feel free to try it out (you can also test it locally): https://github.com/aws-cloudformation/aws-cloudformation-templates/blob/main/CloudFormation/CustomResources/getfromjson/README.md