我想知道如何导入另一个堆栈中定义的安全组,然后在当前堆栈中使用。
到目前为止我已经尝试过这个..
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
}
}
Run Code Online (Sandbox Code Playgroud)
然后从另一个堆栈我想访问导出的安全组 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')
})
Run Code Online (Sandbox Code Playgroud)
然后使用如下
sg_nginx.addIngressRule(sg_relayImp, wowzaPort, 'asg_RelyToNgn_8000')
Run Code Online (Sandbox Code Playgroud)
显然它对我不起作用。
我找不到堆栈之间安全组的任何导入功能,就像 vpc 一样。
任何人都可以帮助我解决这种情况吗?
Kan*_*ane 23
可以直接引用应用中的跨栈资源。
下面是一段代码片段,
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
});
Run Code Online (Sandbox Code Playgroud)
假设有问题的堆栈都在您的 CDK 应用程序下,您可以使用堆栈输出来共享资源。
文档在这里:https : //docs.aws.amazon.com/cdk/api/latest/docs/core-readme.html#stack-outputs
我发现这篇博文作为一个例子很有用(不是我写的)
它应该适用于您可能想要在堆栈之间引用的任何资源。
编辑:这就是我目前正在使用的。
// 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'));
Run Code Online (Sandbox Code Playgroud)
唯一提前“已知”的是您要输出的参数的名称。
这实际上与您在其他答案中提供的内容相同,但 CDK 会Fn.importValue为您编写。
警告:不适用于位于不同区域的堆栈中的资源。该限制是由 CloudFormation 强加的,也将发生在@Kane 的回答中。
SecurityGroup.export您可以在最初定义安全组的堆栈中使用,这将创建一个Output具有生成的导出名称的堆栈,并返回您需要传递到的数据SecurityGroupRef.import,以便获取对其他堆栈中安全组的引用。
您需要确保首先部署定义安全组的堆栈,否则其他堆栈将无法从该堆栈的输出导入。
| 归档时间: |
|
| 查看次数: |
9795 次 |
| 最近记录: |