隔离 VPC 子网中的 Lambda 函数无法访问 SSM 参数

Lambda function in isolated VPC subnet can't access SSM parameter

我非常困惑如何使用 SSM 端点,以便隔离子网上的 Lambda 可以使用 ssm.GetParameter

根据 ,我需要一个用于 SSM 的 VPC 端点。我试过这样做:

// Create a security group:
this.vpcsg = new ec2.SecurityGroup(this, 'vpc-sg', {
  vpc: this.vpc,
  allowAllOutbound: false,
  securityGroupName: 'VPCSecurityGroup'
})

// endpoint creation
this.vpcEndpointSSM = new ec2.InterfaceVpcEndpoint(this, `SSMVpcEndpoint`, {
  service: ec2.InterfaceVpcEndpointAwsService.SSM,
  vpc: this.vpc,
  subnets: { subnetType: ec2.SubnetType.ISOLATED },
  securityGroups: [this.ingressSecurityGroup]
})


// And then later I call...
this.lambdaGQLAPI = new lambda.Function(this, `LambdaAPI`, {
  code: new lambda.AssetCode(lambdaNodePath),
  vpc: this.vpc,
  vpcSubnets: { subnetType: ec2.SubnetType.ISOLATED },
  functionName: this.functions.api,
  handler: 'lambda_graphql.handler',
  memorySize: 256,
  timeout: core.Duration.minutes(2),
  runtime: lambda.Runtime.NODEJS_12_X,
  securityGroups: [props.dbSecurityGroup, this.vpcsg],
})

我还确保 lambda 函数应该能够使用策略模拟器访问 SSM 并检查

但是我的函数在尝试访问 SSM 时超时。

当您不包含 subnets 属性 时,它将默认仅在您的 private 子网中创建 enis

尝试在您的隔离子网中创建 SSM 接口端点

this.vpcEndpointSSM = new ec2.InterfaceVpcEndpoint(this, `SSMVpcEndpoint`, {
    service: ec2.InterfaceVpcEndpointAwsService.SSM,
    subnets: ec2.SubnetSelection(
        subnetType: ec2.SubnetType.ISOLATED
    ),
    vpc: this.vpc
})

安全组单独应用于每个资源。安全组不同于子网。资源不驻留在安全组“内部”。

同一个安全组中的资源不能相互通信,除非安全组中有特定的规则允许访问来自自身.

例如,安全组可以有一个规则,允许入站端口 80,源是同一个安全组。这意味着具有该安全组的资源可以从与同一安全组关联的其他资源接收流量。

但是,通常最好定义两个安全组:

  • Lambda 函数 (Lambda-SG) 上的一个安全组允许所有出站访问
  • VPC 端点 (Endpoint-SG) 上的一个安全组允许来自 Lambda-SG
  • 的入站流量

Endpoint-SG特指Lambda-SG.