[英]Unable to get access to SNS_TOPIC_ARN value from environment in lambda function when defining a cloudFormation script
這是我的雲形成腳本
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /
Method: GET
Policies:
- SNSPublishMessagePolicy:
TopicName: !GetAtt HelloWorldTopic.TopicName
Environment:
Variables:
SNS_TOPIC_ARN: !Ref HelloWorldTopic
HelloWorldTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: benjamintlee600@gmail.com
Protocol: email`enter code here`
這是我的 Lambda function:
const aws = require('aws-sdk');
aws.config.update({ region: 'eu-west-2' });
const sns = new aws.SNS({ region: 'us-east-1' });
exports.handler = async function (event, context) {
console.log('SNS_TOPIC_ARN: ' + process.env.SNS_TOPIC_ARN);
const params = {
Message: 'Hello World!',
Subject: 'SNS Notification from Lambda',
TopicArn: process.env.SNS_TOPIC_ARN,
};
try {
await sns.publish(params).promise();
return { statusCode: 200, body: 'Message sent' };
} catch (err) {
return { statusCode: 500, body: JSON.stringify(err) };
}
};
每次我發出 API 請求時,由於某種原因,我都會收到一條錯誤消息:“無效參數:TopicArn 或 TargetArn 原因:所需參數沒有值”。
任何建議將不勝感激。
我注意到模板的幾個問題。
Environment
and Policies
應位於Type
下方的Properties
下。 目前它的外部Properties
我也相信Policies
格式有點錯誤。 通常它有點像
Policies:
- AWSLambdaExecute
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectACL
Resource: 'arn:aws:s3:::my-bucket/*'
在你的情況下,這將是sns:publish
和類似的東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.