如何使用#AWS-CDK从另一个堆栈导入安全组?

时间:2018-10-22 05:31:33

标签: aws-cdk

我想知道如何导入另一个堆栈中定义的安全组,然后在当前堆栈中使用。

到目前为止,我已经尝试过了。

class relayStack extends cdk.Stack {
    public sg_relay: ec2.SecurityGroupRefProps

    constructor(parent: cdk.App, name: string, props: VPCProps) {
        super(parent, name, props);

        //#IMPORT VPC PROPS
        const vpc = ec2.VpcNetwork.import(this, 'VPC-Hottest100', props.infra.vpc);
        //#AUTOSCALING GROUP
        const asg_relayServer = new ec2.AutoScalingGroup(this, 'ASG_Relay', {
            vpc,
            instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.T2, ec2.InstanceSize.Small),
            minSize: 1,
            maxSize: 3,
            desiredCapacity: 1,
            machineImage: new ec2.GenericLinuxImage({
                "ap-southeast-2": "ami-dc361ebf",
            }),
            keyName: 'icecast-poc',
            allowAllOutbound: false,
            vpcPlacement: {
                usePublicSubnets: false
            }
        });

        //#SECURITY Group
        const sg_relay = new ec2.SecurityGroup(this, 'SG_RELAY', {
            vpc,
            description: "Relay stack security group",
            groupName: 'relay-sg'
        })


        this.sg_relay = sg_relay
    }
}

然后从另一个堆栈中,我要访问导出的安全组sg_relay

我已经尝试关注

//#SECURITY GROUP
const sg_nginx = new ec2.SecurityGroup(this, "SG_NGINX", {
    vpc,
    description: "NGINX stack security group",
    groupName: 'nginx-sg'
})

const sg_relayImp = new ec2.SecurityGroupRef(this, "SG_RELAY_IMP", {
    securityGroupId: new ec2.SecurityGroupId('SG_RELAY')
})

然后使用以下内容

sg_nginx.addIngressRule(sg_relayImp, wowzaPort, 'asg_RelyToNgn_8000')

显然对我不起作用。

我找不到堆栈之间的安全组的任何导入功能,例如vpc有一个。

有人可以帮助我解决这种情况吗?

4 个答案:

答案 0 :(得分:3)

您可以直接在应用程序中引用交叉堆栈资源。

下面是一个代码段,

export class InfraCdkStack extends cdk.Stack {
  // Create a readonly property to reference on an instance.
  readonly vpc: ec2.IVpc;

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here.
    // Assign your vpc to your previously created property.
    // Creates a vpc in two AZs.
    this.vpc = new ec2.Vpc(this, 'MyVPC');
  }
}

// Create an interface to hold the vpc information.
interface ECSStackProps extends cdk.StackProps {
  vpc: ec2.IVpc;
}

// Have your class constructor accept the interface.
export class ECSCdkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: ECSStackProps) {
    super(scope, id, props);
}

const app = new cdk.App();
const infraStack = new InfraCdkStack(app, 'InfraCdkStack');
// Pass the infraStack.vpc property to the ECSCdkStack class.
const gameECSStack = new ECSCdkStack(app, 'ECSCdkStack', {
    vpc: infraStack.vpc
});

答案 1 :(得分:2)

在堆栈 A 中创建一个这样的示例安全组。

const sampleSecurityGroup = new ec2.SecurityGroup(this, 'security-group', { vpc: vpc, allowAllOutbound: true, description: 'Security Group Sample', securityGroupName: "SAMPLE-SG" });

使用下面的堆栈 A 导出 SG。

const myoutput = new cdk.CfnOutput(this, 'Security-group-id-output', { description: 'Security group in Stack A', exportName: 'security-id-output', value: sampleSecurityGroup.securityGroupId });

在 UI 中检查 Cloud Forming 服务,您应该会看到名为“security-id-output”的导出。

在堆栈 B 中使用

导入值
cdk.Fn.importValue("security-id-output");

答案 2 :(得分:0)

您可以在最初定义安全组的堆栈中使用SecurityGroup.export,这将创建一个具有生成的导出名称的堆栈Output,并将需要传递给{{ 1}},以获得对另一个堆栈中安全组的引用。

您需要确保首先部署定义安全组的堆栈,否则其他堆栈将无法从该堆栈的输出中导入。

答案 3 :(得分:0)

假设有问题的堆栈都在CDK应用程序下,则可以使用堆栈输出共享资源。

此处的文档:https://docs.aws.amazon.com/cdk/api/latest/docs/core-readme.html#stack-outputs

我发现this blog post可以作为示例(不是我写的)

应该适用于您可能希望在堆栈之间引用的任何资源。

编辑:这是我目前正在使用的。

// I have a resource which is a cloudfront dist id in StackA
new cdk.CfnOutput(this, 'cloudfront-dist-id-output', {
      description: 'cloudfront-dist-id-output',
      exportName: 'cloudfront-dist-id-output',
      value: cloudFrontDistribution.distributionId
    });

// Stack B needs the DistributionId (it's dynamic), so I pass it in as a parameter.
new StackB(app, 'StackB', Fn.importValue('cloudfront-dist-id-output'));

事先唯一的“已知”是输出的参数的名称。

这实际上与您在其他答案中提供的内容相同,但是CDK为您编写了Fn.importValue

警告:不适用于位于不同区域的堆栈中的资源。该限制是由CloudFormation施加的,也将在@Kane的答案中发生。

相关问题