如何将对现有 VPC 的引用传递给 cloudformation 模板?

how to pass reference to existing VPC to a cloudformation template?

我正在创建一个 cloudformation 模板,资源很少,几个 lambda 函数,下面的 S3 bucket.see 代码,它正在取得进展,到目前为止我有一个 S3 存储桶和一个由 S3 触发的 lamda 函数.我们在我们的团队中定义了我们应该使用的 vpc。我想在该 vpc 下为我的 lambda 函数添​​加私有子网,并为 s3 存储桶分配 public 子网。如何获取 vpc 的引用,并将其传递给我的模板并使用它?示例代码会有所帮助。

AWSTemplateFormatVersion: 2010-09-09
Resources:

  # S3 Bucket
  S3Bucket:
    Type: AWS::S3::Bucket

  # Functions
  S3-Lambda-trigger:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: lambda.handler
      Description: s3 object creation triggers lambda
      Runtime: nodejs12.x
      Events:
        S3Bucket:
          Type: S3
          Properties:
            Bucket: !Ref S3Bucket
            Events: 's3:ObjectCreated:*'

  # Permissions
  Allow-lamda-invocation-s3:
    Type: AWS::Lambda::Permission
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref S3-Lambda-trigger
      Principal: s3.amazonaws.com
      SourceArn: !GetAtt S3Bucket.Arn

how to get reference of the vpc , and pass it to my template and use it?

一种方法是通过 AWS-Specific Parameter Types,特别是 AWS::EC2::VPC::Id,在 Parameters 部分。

例如:

AWSTemplateFormatVersion: 2010-09-09

Parameters: 

  VPCId: 
    Type: AWS::EC2::VPC::Id

Resources:

  MySubnet:
    Type: AWS::EC2::Subnet
    Properties: 
      # other properties
      VpcId: !Ref VPCId

因此,在 AWS 控制台中创建堆栈时,您可以选择 existing VPCId 传递给模板。