在 Cloudformation 模板中,我如何在 IoT Rule 中引用动态生成的 Lambda 函数 ARN?

问题描述 投票:0回答:1

使用 AWS Amplify CLI,我已经为我的项目创建了一个 Lambda 函数。作为该过程的一部分,它创建了一个Cloudformation模板。我正在编辑模板,添加一个IoT规则来触发lambda函数。每个环境的函数名称本身都会改变,还有我在 IoT 规则部分试图瞄准的 Lambda 函数 ARN。

这是我现在正在做的部分。

"IoTRuleS3RequestSignedUrl": {
    "Type": "AWS::IoT::TopicRule",
    "Properties": {
        "RuleName": "twinTigerSecurityS3SignedUrlRequests",
        "TopicRulePayload": {
            "Actions": [
                {
                    "Lambda": {
                        "FunctionArn": "HOW DO I REFERENCE THIS DYNAMIC ARN?"
                    }
                }
            ],
            "Description": "Get S3 bucket signed URL to upload image directly to S3.",
            "RuleDisabled": false,
            "Sql": "SELECT operation, bucket, key, replyTo FROM 'iot/topic'"
        }
    }
} 

这是我正在做的完整模板

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Lambda resource stack creation using Amplify CLI",
    "Parameters": {
        "CloudWatchRule": {
            "Type": "String",
            "Default" : "NONE",
            "Description" : " Schedule Expression"
        },
        "env": {
            "Type": "String"
        }

    },
    "Conditions": {
        "ShouldNotCreateEnvResources": {
            "Fn::Equals": [
                {
                    "Ref": "env"
                },
                "NONE"
            ]
        }
    },
    "Resources": {
        "LambdaFunction": {
        "Type": "AWS::Lambda::Function",
        "Metadata": {
            "aws:asset:path": "./src",
            "aws:asset:property": "Code"
        },
        "Properties": {
            "Handler": "index.handler",
            "FunctionName": {
                "Fn::If": [
                    "ShouldNotCreateEnvResources",
                    "twinTigerSecurityRequestS3SignedUrl", 
                    {

                        "Fn::Join": [
                            "",
                            [
                                "twinTigerSecurityRequestS3SignedUrl",
                                "-",
                                {
                                    "Ref": "env"
                                }
                            ]
                        ]
                    }      
                ]
            },
            "Environment": {
                "Variables" : {
                    "ENV": {
                        "Ref": "env"
                    },
                    "REGION": { 
                        "Ref": "AWS::Region"
                    }

                }
            },
            "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
            "Runtime": "nodejs12.x",
            "Timeout": "25"
        }
        },
        "LambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "RoleName": {
                    "Fn::If": [
                        "ShouldNotCreateEnvResources",
                        "twintigersecurityLambdaRolebf1a383b", 
                        {

                            "Fn::Join": [
                                "",
                                [
                                    "twintigersecurityLambdaRolebf1a383b",
                                    "-",
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        } 
                    ]
                },
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                }
            }
        }
        ,"lambdaexecutionpolicy": {
            "DependsOn": ["LambdaExecutionRole"],
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "lambda-execution-policy",
                "Roles": [{ "Ref": "LambdaExecutionRole" }],
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action":["logs:CreateLogGroup",
                            "logs:CreateLogStream",
                            "logs:PutLogEvents"],
                            "Resource": { "Fn::Sub" : [ "arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*", { "region": {"Ref": "AWS::Region"},  "account": {"Ref": "AWS::AccountId"}, "lambda": {"Ref": "LambdaFunction"}} ]}
                        }
                    ]
                }
            }
        },
        "IoTRuleS3RequestSignedUrl": {
            "Type": "AWS::IoT::TopicRule",
            "Properties": {
                "RuleName": "twinTigerSecurityS3SignedUrlRequests",
                "TopicRulePayload": {
                    "Actions": [
                        {
                            "Lambda": {
                                "FunctionArn": "HOW DO I REFERENCE THIS DYNAMIC ARN?"
                            }
                        }
                    ],
                    "Description": "Get S3 bucket signed URL to upload image directly to S3.",
                    "RuleDisabled": false,
                    "Sql": "SELECT operation, bucket, key, replyTo FROM 'iot/topic'"
                }
            }
        }              
    },
    "Outputs": {
        "Name": {
            "Value": {
                "Ref": "LambdaFunction"
            }
        },
        "Arn": {
            "Value": {"Fn::GetAtt": ["LambdaFunction", "Arn"]}
        },
        "Region": {
            "Value": {
                "Ref": "AWS::Region"
            }
        },
        "LambdaExecutionRole": {
            "Value": {
                "Ref": "LambdaExecutionRole"
            }
        }

    }
}

我可以在UI中做到这一点,然而这不是理想的长期,也不是由AmplifyCloudformation提供的代码配置的意图。从物联网规则中引用Lambda函数的最佳方式是什么?

amazon-web-services amazon-cloudformation aws-amplify
1个回答
2
投票

您可以使用固有函数 Fn::GetAtt 获取资源的ARN,如下所示。

"Fn::GetAtt": ["LambdaFunction", "Arn"]

© www.soinside.com 2019 - 2024. All rights reserved.