【问题标题】:how to put the lambda in VPC by CloudFormation template如何通过 CloudFormation 模板将 lambda 放入 VPC
【发布时间】:2022-02-04 23:41:25
【问题描述】:

我正在开发 lambda 与 CloudFormation by SAM

我的template.yaml 在这里。

可以部署,但是这个 lambda 没有在 VPC 中设置。

我想将 lambda 放入默认 VPC(以访问 RDS)

任何设置都可以在这里使用,或者我应该做其他事情??

(而且,模板自动生成IAmRole,有什么办法可以附加策略吗?例如RDSFullAccess

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  python3.9   Sample SAM Template for chatbot-sam

Parameters:
  DBNAME:
    Type: String
  DBUSER:
    Type: String
  DBPASSWORD:
    Type: String
  DBHOST:
    Type: String
  DBPORT:
    Type: String
  LINELONGLIVETOKEN:
    Type: String
Globals:
  Function:
    Timeout: 30
    Environment:
      Variables:
        DBNAME: !Ref DBNAME
        DBUSER: !Ref DBUSER
        DBPASSWORD: !Ref DBPASSWORD
        DBHOST: !Ref DBHOST
        DBPORT: !Ref DBPORT  
        LINELONGLIVETOKEN: !Ref LINELONGLIVETOKEN  
Resources:
  WebhookFunction:
    Type: AWS::Serverless::Function 
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        Webhook:
          Type: Api 
          Properties:
            Path: /webhook
            Method: post
    Metadata:
      Dockerfile: Dockerfile.webhook
      DockerContext: ./chatbotapp
      DockerTag: python3.9-v1




Outputs:
  WebhookApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/webhook/"
  WebhookFunction:
    Description: "Webhook Lambda Function ARN"
    Value: !GetAtt WebhookFunction.Arn
  WebhookFunctionIamRole:
    Description: "Implicit IAM Role created for Webhook function"
    Value: !GetAtt WebhookFunctionRole.Arn

我更新了。

附加 VpcConfigPolicies ,但看起来没有变化。

lambda -> setting -> vpc,没有vpc设置,找不到属于SecurityGroup和Subnet的线索

  Policies: AWSLambdaVPCAccessExecutionRole
  VpcConfig:
    SubnetIds:
      - subnet-fb6fa4d0
      - subnet-bf8ab8e4
    SecurityGroupIds:
      - sg-0641506b4ec3782de


AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  python3.9   Sample SAM Template for chatbot-sam

Parameters:
  DBNAME:
    Type: String
  DBUSER:
    Type: String
  DBPASSWORD:
    Type: String
  DBHOST:
    Type: String
  DBPORT:
    Type: String
  LINELONGLIVETOKEN:
    Type: String
Globals:
  Function:
    Timeout: 30
    Environment:
      Variables:
        DBNAME: !Ref DBNAME
        DBUSER: !Ref DBUSER
        DBPASSWORD: !Ref DBPASSWORD
        DBHOST: !Ref DBHOST
        DBPORT: !Ref DBPORT  
        LINELONGLIVETOKEN: !Ref LINELONGLIVETOKEN  
Resources:
  WebhookFunction:
    Type: AWS::Serverless::Function 
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        Webhook:
          Type: Api 
          Properties:
            Path: /webhook
            Method: post
      Policies: AWSLambdaVPCAccessExecutionRole
      VpcConfig:
        SubnetIds:
          - subnet-fb6fa4d0
          - subnet-bf8ab8e4
        SecurityGroupIds:
          - sg-0641506b4ec3782de
    Metadata:
      Dockerfile: Dockerfile.webhook
      DockerContext: ./chatbotapp
      DockerTag: python3.9-v1




Outputs:
  WebhookApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/webhook/"
  WebhookFunction:
    Description: "Webhook Lambda Function ARN"
    Value: !GetAtt WebhookFunction.Arn
  WebhookFunctionIamRole:
    Description: "Implicit IAM Role created for Webhook function"
    Value: !GetAtt WebhookFunctionRole.Arn

【问题讨论】:

  • 我很想拆除你的堆栈,如果这不是过度的负担,然后在你添加了 VpcConfig 后从头开始重新部署。

标签: amazon-web-services amazon-cloudformation aws-cdk


【解决方案1】:

您需要将VpcConfig 添加到函数定义的属性中。您可以查看一个如何使用 here 的示例。

您还可以将策略添加到为函数创建的默认角色,或者您可以提供自己的角色,在这种情况下,不会创建默认角色。

【讨论】:

  • 非常感谢你给了我很好的提示,我添加了Policies和VpcConfig,它可以正确部署,但我看不到它属于VPC,因为打开lambda- >setting->VPC 什么都没有。我更新了文章
  • 对此我没有很好的解释。如果您正在查看正确的函数,并且它确实已成功部署,则 VPC 设置应该是可见的。您可以尝试使用 CLI 查询该函数,看看它是否只是控制台的问题。
  • 非常感谢。我明白,因此它已正确部署。我正在检查的是lambda->setting->VPC 显示另一个参数,我误解了。我的问题解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 2016-04-23
  • 2020-10-22
  • 2021-09-16
  • 2020-08-16
相关资源
最近更新 更多