【问题标题】:How to set "Remote IPv4 Network CIDR" VPN properties using cloudformation in AWS如何在 AWS 中使用 cloudformation 设置“远程 IPv4 网络 CIDR”VPN 属性
【发布时间】:2021-09-23 08:00:15
【问题描述】:

我们使用 cloudformation 作为基础设施代码,用于本地和 AWS 账户之间的 VPN 连接。我们需要设置一个记录为 (complete docs) 的参数:

远程 IPv4 网络 CIDR (仅限 IPv4 VPN 连接)AWS 端允许通过 VPN 隧道进行通信的 IPv4 CIDR 范围。 默认值:0.0.0.0/0

我们已经在互联网上搜索过,但对于 cloudformation 如何设置该变量并没有真正的语法。

我们希望将默认值 0.0.0.0/0 的值设置为另一个更具体的 /24 范围。

在某些 VPN 软件中,这被称为流量选择器、代理 ID 或加密域。

【问题讨论】:

  • 您的文档链接并不具体。参数具体定义在哪里?
  • @Marcin 现在链接更好了。它是“远程 IPv4 网络 CIDR”参数。
  • 这些是否设置在 vpn 配置文件中?这不是在 CloudFormation 级别设置的。
  • @Marcin 设置此属性后,我们可以从 AWS 控制台下载配置,然后将其放入我们的本地 VPN 软件。我能说的并非相反。
  • 是使用自定义资源的唯一方法吗?

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


【解决方案1】:

可以使用 sdk 更改远程 IPv4 网络 CIDR。下面的云结构将更改远程 IPv4 网络 CIDR。

    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: root
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Action:
                 - logs:*
                Resource: arn:aws:logs:*:*:* // Set appropriate value
              - Effect: Allow
                Action:
                 - ec2:ModifyVpnConnectionOptions
                Resource: !Sub "arn:aws:ec2:*:..." // Refere to your AWS::EC2::VPNConnection

    # A Lambda that changes the remote Ipv4 property of VPN using the aws sdk.
    # Asynchronous, so it will finish before the modification of the VPN is done.
    customResourceSetRemoteIp:
        Type: AWS::Lambda::Function
        Properties:
          Runtime: nodejs14.x
          Role: !GetAtt lambdaExecutionRole.Arn
          Handler: index.handler
          Code:
            ZipFile: |
                var response = require('cfn-response')
                var aws = require('aws-sdk')
                exports.handler = function (event, context) {
                    console.log("REQUEST RECEIVED:\n" + JSON.stringify(event))
                    
                    // For Delete requests, immediately send a SUCCESS response.
                    // You need to run this job with the new value if you want a rollback. 
                    if (event.RequestType == "Delete") {
                        response.send(event, context, "SUCCESS")
                        return
                    }
                    var responseStatus = "FAILED"
                    var responseData = {}
                    var vpnConnection = event.ResourceProperties.VpnConnection;
                    var remoteIpv4NetworkCidr = event.ResourceProperties.RemoteIpv4NetworkCidr;
                    
                    console.log("Set remote ipv4 cidr to '" + remoteIpv4NetworkCidr + 
                        "' at vpn connection '" + vpnConnection + "'");
                    
                    var ec2 = new aws.EC2();
                    var params = {
                      VpnConnectionId: vpnConnection, /* required */
                      DryRun: false,
                      RemoteIpv4NetworkCidr: remoteIpv4NetworkCidr
                    };
                    ec2.modifyVpnConnectionOptions(params, function(err, data) {
                      if (err) {
                          console.log(err, err.stack); // an error occurred
                          responseData = {Error: err}
                          console.log(responseData.Error + ":\n", err)
                      } else {
                          responseStatus = "SUCCESS"
                          console.log(data);           // successful response
                      }
                      response.send(event, context, responseStatus, responseData)
                    });
                }
          Description: Set VPN options in cloudformation
          TracingConfig:
            Mode: PassThrough

    setRemoteIpOnVpnCustomResource:
        Type: AWS::CloudFormation::CustomResource
        Version: "1.0"
        Properties:
          ServiceToken: !GetAtt customResourceSetRemoteIp.Arn
          VpnConnection: !Ref vpcVpnConnection
          RemoteIpv4NetworkCidr: "10.0.0.0/24"

【讨论】:

  • 看起来很有趣,对我们来说它最适合使用简单的内联代码。我会测试一下!
  • 工作得很好,正如您在代码中评论的那样,资源是异步的,因此需要查看 aws 控制台以查看修改何时完成。 ?
猜你喜欢
  • 2017-06-13
  • 2021-05-31
  • 2020-05-10
  • 2018-02-28
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多