【发布时间】:2021-09-29 10:51:54
【问题描述】:
我正在使用 AWS Lambda、API Gateway 和 RDS (MySQL) 开发一个 REST API。我正在使用 Node.js。
为了保护数据库凭证,我访问了 AWS Web 控制台并创建了一个新密钥。
现在我需要在我的 Lambda 函数中访问这些。我知道我必须分配SecretsManagerReadWrite 权限,但我不知道该怎么做。
下面是我的 CloudFormation 配置文件。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
aaaa-restapi
Sample SAM Template for aaaa-restapi
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 100
VpcConfig:
SecurityGroupIds:
- sg-041f2455252528e
SubnetIds:
- subnet-0385252525
Resources:
GetAllAccountingTypesFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: aaaa-restapi/
Handler: accountingtypes-getall.getallaccountingtypes
Runtime: nodejs14.x
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /accountingtypes/getallaccountingtypes
Method: get
LambdaRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ec2:DescribeNetworkInterfaces
- ec2:CreateNetworkInterface
- ec2:DeleteNetworkInterface
- ec2:DescribeInstances
- ec2:AttachNetworkInterface
Resource: '*'
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for functions"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
在此文件中,我如何向我的 Lambda 函数授予权限?
【问题讨论】:
-
您只需获取 lambda 函数中的秘密。它与云的形成无关。
-
@Marcin:谢谢你。请提供您的评论作为答案。
-
@Marcin:还有一个问题,你知道如何从
secret获取特定值吗?我可以得到整个秘密,不是问题,而是得到具体的价值? -
很遗憾,您不能请求秘密的一部分。你得到它的全部价值,或者什么都没有。
-
是的,我知道。我的意思是,在得到秘密之后,我应该能够做类似
secret.password之类的事情,对吧?否则我无法添加密码来访问数据库等等......
标签: node.js amazon-web-services amazon-cloudformation aws-sam aws-secrets-manager