0
votes

I have created a yaml template file. I want to tag a lambda function based on a condition as given in the documentation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html

Following is my yaml-

AWSTemplateFormatVersion: '2012-10-10'  
Description: "Cloud formation template"

Parameters:  
      tagName:  
        Type: String  
        Description: "tag name for the resource"  
        Default: test  

Conditions:  
      isConditionalTag:  
        - Fn::Equals:  
            - Ref: tagName  
            - "test"  
Resources:  
       TestambdaFunction:  
       Properties:  
            Code:  
                 S3Bucket: "abc-test"  
                 S3Key: xyz-RELEASE.jar  
            Description: Test Lambda function  
            Environment:  
              Variables:  
                DATA_TYPE: "test-data"  
            FunctionName: TestFunction  
            Handler: com.test.testLambda::handleRequest  
            MemorySize: 200  
            Role: "arn:aws:iam::user:role/general"  
            Runtime: java8  
            Timeout: 300  
            Tags:  
            -    Key: "component"  
                 Value:  
                    Fn::If:  
                        - isConditionalTag  
                        - Ref: tagName  
                        - "newValue"  
       Type: AWS::Lambda::Function 

There are no formatting errors while runnning the template using boto gives validation error as

boto.exception.BotoServerError: BotoServerError: 400 Bad Request {"Error":{"Code":"ValidationError","Message":"Template format error: Conditions can only be boolean operations on parameters and other conditions","Type":"Sender"},"RequestId":"30250a23-4a66-11e8-a3bd-a14cac12563"}

1

1 Answers

0
votes

You won't need to use a Condition to get this done.

Conditions are used to define whether or not a resource is created in Cloudformation.

Based on the documentation,

The optional Conditions section includes statements that define when a resource is created or when a property is defined. For example, you can compare whether a value is equal to another value. Based on the result of that condition, you can conditionally create resources

You should be able to solve this doing something like this (untested):

Parameters:  
  tagName:  
    Type: String  
    Description: "tag name for the resource"  
    Default: test  
Resources:  
  TestambdaFunction:  
    Properties:  
      Code:  
        S3Bucket: "abc-test"  
        S3Key: xyz-RELEASE.jar  
      Description: Test Lambda function  
      Environment:  
        Variables:  
          DATA_TYPE: "test-data"  
      FunctionName: TestFunction  
      Handler: com.test.testLambda::handleRequest  
      MemorySize: 256  
      Role: "arn:aws:iam::user:role/general"  
      Runtime: java8  
      Timeout: 300
      Tags:  
        - Key: "component"  
          Value: !Ref tagName
    Type: AWS::Lambda::Function

Note that Lambda functions need memory in increments of 64MB