AWS CloudFormation模板EC2 - 根据VPC选择返回子网列表
创始人
2024-11-14 19:00:29
0

以下是一个使用AWS CloudFormation模板创建EC2实例并根据VPC选择返回子网列表的示例:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: Select the VPC to launch the EC2 instance in
  SubnetId:
    Type: AWS::EC2::Subnet::Id
    Description: Select the subnet to launch the EC2 instance in
Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-xxxxxxxxx
      InstanceType: t2.micro
      SubnetId: !Ref SubnetId
  SubnetListFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          import boto3
          def lambda_handler(event, context):
              vpc_id = event['ResourceProperties']['VpcId']
              ec2_client = boto3.client('ec2')
              response = ec2_client.describe_subnets(
                  Filters=[
                      {
                          'Name': 'vpc-id',
                          'Values': [vpc_id]
                      }
                  ]
              )
              subnet_list = []
              for subnet in response['Subnets']:
                  subnet_list.append(subnet['SubnetId'])
              return subnet_list
      Handler: index.lambda_handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Runtime: python3.8
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      Policies:
        - PolicyName: LambdaExecutionPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DescribeSubnets
                Resource: '*'
  SubnetListOutput:
    Type: Custom::SubnetListOutput
    Properties:
      ServiceToken: !GetAtt SubnetListFunction.Arn
      VpcId: !Ref VpcId
Outputs:
  SubnetList:
    Value: !GetAtt SubnetListOutput.SubnetList
    Description: List of subnets in the selected VPC

在这个示例中,模板定义了两个参数:VpcId和SubnetId。VpcId参数用于选择VPC,SubnetId参数用于选择子网。EC2Instance资源根据SubnetId参数创建一个EC2实例。

模板还定义了一个Lambda函数(SubnetListFunction),它使用boto3库查询给定VPC的子网列表。该函数返回子网列表。

Lambda函数需要一个IAM角色(LambdaExecutionRole)来执行ec2:DescribeSubnets操作。该角色使用AssumeRolePolicyDocument指定允许Lambda服务扮演该角色的权限,并使用Policies指定允许操作的权限。

最后,模板还定义了一个Custom资源(SubnetListOutput),它使用SubnetListFunction来获取子网列表,并输出为CloudFormation输出(SubnetList)。

要使用此模板,您可以在AWS Management Console的CloudFormation服务中创建一个新堆栈,并提供所需的参数(VpcId和SubnetId)。然后,您将能够查看输出中的子网列表。

相关内容

热门资讯

透视揭露!wepoker模拟器... 透视揭露!wepoker模拟器哪个,we-poker靠谱吗-竟然存在有辅助神器(哔哩哔哩)在进入软件...
透视辅助!hhpoker有没有... 透视辅助!hhpoker有没有作弊辅助,hhpoker是内部控制吗-总是是有辅助软件(哔哩哔哩)1、...
透视普及!aapoker透视插... 透视普及!aapoker透视插件,aapoker万能辅助器-好像一直总是有辅助神器(哔哩哔哩)一、可...
透视解谜!wpk控制牌是真的吗... 透视解谜!wpk控制牌是真的吗,微扑克微乐辅助-真是有辅助神器(哔哩哔哩)1、打开软件启动之后找到中...
透视解谜!wpk透视辅助方法,... 透视解谜!wpk透视辅助方法,wpk德州局透视-本来是有辅助脚本(哔哩哔哩)1、用户打开应用后不用登...
透视普及!wepokerh5破... 透视普及!wepokerh5破解,wepokerplus透视脚本免费-都是一直总是有辅助技巧(哔哩哔...
透视解谜!wepoker黑侠破... 透视解谜!wepoker黑侠破解,wepoker作弊视频-好像一直都是有辅助攻略(哔哩哔哩);1、打...
透视开挂!we-poker辅助... 透视开挂!we-poker辅助,wepoker有辅助器吗-都是存在有辅助脚本(哔哩哔哩)1、辅助软件...
透视详情!wepokerplu... 透视详情!wepokerplus透视脚本免费,wepoker辅助器是真的吗-好像一直总是有辅助方法(...
透视总结!wpk安卓下载辅助,... 透视总结!wpk安卓下载辅助,wpk辅助插件-原来存在有辅助插件(哔哩哔哩)一、游戏安装教程牌型概率...