AWS CloudFormation模板EC2 - 根据VPC选择返回子网列表
创始人
2024-11-14 19:02:08
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)。然后,您将能够查看输出中的子网列表。

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...