0

I need to refer already created ALB into MY SAM Template to do that i add AWS::ElasticLoadBalancingV2::TargetGroupAttachment resource and do the needfull.

Then when im deploy it using SAM CLI it will getting error by saying that ->

[ ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression "Status" we matched expected path: "FAILED" Status: FAILED. Reason: Template format error: Unrecognized resource types: [AWS::ElasticLoadBalancingV2::TargetGroupAttachment]

how can i edit bellow SAM template do create working one

This my SAM Template i did try.

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  pp-routing-parcel-event

  Sample SAM Template for pp-routing-parcel-event


Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  SNSRouter:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: parcel-topic-3
  SQSHandler:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: hubbed-parcel-event-integration-3
  SQSQueuePolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref SQSHandler
      PolicyDocument:
        Version: "2008-10-17"
        Id: SQSQueuePolicy
        Statement:
          - Effect: Allow
            Principal:
              AWS: "*"
            Action:
              - SQS:SendMessage
              - SQS:ReceiveMessage
            Resource:
              - !GetAtt SQSHandler.Arn
            Condition:
              ArnLike:
                aws:SourceArn: !Ref SNSRouter

  SNSTopicSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: SQS
      Endpoint: !GetAtt SQSHandler.Arn
      TopicArn: !Ref SNSRouter

  PpLambdaExecutionRole:
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: pp-lambda-execution-role
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 
      # ManagedPolicyArns : use to get the pre-define Cloudwatch read logs
      
      Policies:
        - PolicyName: PPSNSPublishPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - sns:Publish
                Resource: !Ref SNSRouter

  PpLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: pp-lambda-function-3
      Handler: index.handler
      Role: !GetAtt PpLambdaExecutionRole.Arn
      Runtime: nodejs14.x
      InlineCode: |
      
        const AWS = require("aws-sdk");
        const sns = new AWS.SNS({ region: "ap-southeast-2" }); // replace with your region
        const TOPIC_ARN = "I added in my coded"; // replace with your topic ARN

        exports.handler = async (event) => {
          const message = JSON.parse(event.body);
          const params = {
            Message: JSON.stringify(message),
            TopicArn: TOPIC_ARN,
          };
          try {
            const result = await sns.publish(params).promise();
            if (result.MessageId) {
              const sucsessRes = {
                status: message.status,
                trackingLabel: message.trackingLabel,
              };
              return {
                statusCode: 200,
                statusDescription: "HTTP OK",
                isBase64Encoded: false,
                headers: {
                  "Content-Type": "application/json",
                },
                body: JSON.stringify(sucsessRes),
              };
            } else {
              throw new Error(
                "Unknown error occurred while publishing message to SNS topic"
              );
            }
          } catch (error) {
            
            return {
              statusCode: 500,
              statusDescription: "Internal Server Error",
              isBase64Encoded: false,
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify({ message: `Error publishing message to topic ${TOPIC_ARN}`, error }),
            };
          }
        };

  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    DependsOn: PpHbFunctionInvokePermission
    Properties:
      TargetType: lambda
      Targets:
      - Id: !GetAtt PpLambdaFunction.Arn

  PpHbTargetGroupAttachment:
    Type: 'AWS::ElasticLoadBalancingV2::TargetGroupAttachment'
    Properties:
      TargetGroupArn: !Ref TargetGroup
      Target: !GetAtt PpLambdaFunction.Arn

  PpSNSTopicPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName: !GetAtt PpLambdaFunction.Arn
      Principal: sns.amazonaws.com
      SourceArn: !Ref SNSRouter  

  PpHbFunctionInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt PpLambdaFunction.Arn
      Action: 'lambda:InvokeFunction'
      Principal: elasticloadbalancing.amazonaws.com
   
nisalap
  • 122
  • 6

0 Answers0