适用于Lambda和SNS的AWS CloudFormation主题 [英] AWS CloudFormation for Lambda and SNS Topic

查看:108
本文介绍了适用于Lambda和SNS的AWS CloudFormation主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云形成脚本,用于创建Lambda和SNS主题.

I have a cloud formation script which I am using to create a Lambda and SNS Topic.

这是用于SNS主题和Lambda创建的yml脚本,

Here is the yml script for SNS Topic and Lambda creation,

SampleSNSTopic:
Type: AWS::SNS::Topic
Properties:
  DisplayName: sampleTopic
  TopicName: sampleTopic
SampleLambdaFunction:
Type: AWS::Lambda::Function
DependsOn: SampleSNSTopic
Properties:
  Handler: index.handler
  Description: Sample Lambda function
  FunctionName: TestFunction
  Role: !Ref SomeRole
  Code:
    ZipFile: !Sub |
      var AWS = require("aws-sdk");
      exports.handler = function(event, context) {
          var eventText = JSON.stringify(event, null, 2);
          var sns = new AWS.SNS();
          var params = {
              Message: eventText,
              TopicArn: !Ref SampleSNSTopic
          };
          sns.publish(params, context.done);
      };
  Runtime: nodejs6.10
  Timeout: 300
  MemorySize: 512

问题:对主题ARN使用!Ref失败,

Question: Using a !Ref on topic ARN fails,

TopicArn: !Ref SampleSNSTopic

这是正确的方法吗?还是有其他方法可以使用我的SNS主题的ARN在云形成中创建lambda?

Is this the right way to do it? Or is there some other way where I can use my SNS topic's ARN to create lambda in cloud formation?

推荐答案

这类似于此问题的答案:

This is something like the answer to this question:

CloudFormation-来自Lambda代码的访问参数

基本上,您将Ref值分配给环境键/值:

Essentially you assign the Ref value to an Environment key/value:

Properties:
  Handler: index.handler
  Description: Sample Lambda function
  FunctionName: TestFunction
  Environment:
    Variables:
      SNS_TOPIC_ARN: !Ref SampleSNSTopic

然后您可以在Lambda中访问它:

Then you can access that within the Lambda:

  Code:
    ZipFile: !Sub |
      var AWS = require("aws-sdk");
      exports.handler = function(event, context) {
          var eventText = JSON.stringify(event, null, 2);
          var sns = new AWS.SNS();
          var params = {
              Message: eventText,
              TopicArn: process.env.SNS_TOPIC_ARN
          };
          sns.publish(params, context.done);
      };

这篇关于适用于Lambda和SNS的AWS CloudFormation主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆