amazon-web-services - 如何避免 CDK 合成器中的参数化云形成
问题描述
我正在开发一个 CDK 应用程序,我必须在其中创建一个 VPC 和 EKS 集群。但我没有直接使用 CDK 来运行 cloudformation。我想单独创建一个 cloudformation 模板并使用 AWS CLI 运行它。但是每当我创建 cloudformation 模板时,EKS 都有资产参数,这会在我运行模板时导致错误。如何避免这些参数。
这些是我的文件。 bin/eks.ts
#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import { VPCStack, EKSStack } from '../lib/eks-stack';
import { Construct, TagManager, Tag } from '@aws-cdk/core';
import { Scope } from 'babel__traverse';
const app = new cdk.App();
const environment_variables = { region: "us-east-1", account: "348394859384543" }
const appVPCStack = new VPCStack(app, "appDemoVPCStack", { env: environment_variables })
Tag.add(appVPCStack, "owner", "tamizh");
Tag.add(appVPCStack, "purpose", "testing");
const appEKSStack = new EKSStack(app, "appDemoEKSStack", { env: environment_variables, vpcStack: appVPCStack })
Tag.add(appEKSStack, "owner", "tamizh");
Tag.add(appEKSStack, "purpose", "testing");
app.synth();
库/eks.ts
import cdk = require('@aws-cdk/core');
import ec2 = require('@aws-cdk/aws-ec2');
import { DefaultInstanceTenancy, GatewayVpcEndpointAwsService, GatewayVpcEndpoint } from '@aws-cdk/aws-ec2';
import { ManagedPolicy } from '@aws-cdk/aws-iam';
import eks = require('@aws-cdk/aws-eks');
import iam = require('@aws-cdk/aws-iam');
import asg = require("@aws-cdk/aws-autoscaling");
import { TagManager, TagType } from '@aws-cdk/core';
export class VPCStack extends cdk.Stack {
public readonly vpc: ec2.Vpc;
private endpoint: GatewayVpcEndpoint;
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const eksClusterName = this.node.tryGetContext("eks.clusterName");
this.vpc = new ec2.Vpc(this, eksClusterName+'VPC', {
// VPC configurations
})
}
}
export interface EKSProps extends cdk.StackProps {
vpcStack: VPCStack;
}
export class EKSStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: EKSProps) {
super(scope, id, props);
const vpc = props.vpcStack.vpc;
// Context variables for dynamic configuration
const current_env = this.node.tryGetContext("env.type");
const eksClusterName = this.node.tryGetContext("eks.clusterName");
const nodeGroupKeyName = this.node.tryGetContext("eks.nodeGroupKeyName");
const nodeGroupMaxCapacity = this.node.tryGetContext("eks.nodeGroupMaxCapacity");
const nodeGroupMinCapacity = this.node.tryGetContext("eks.nodeGroupMinCapacity");
const nodeGroupDesiredCapacity = this.node.tryGetContext("eks.nodeGroupDesiredCapacity");
const nodeGroupInstanceType = this.node.tryGetContext("eks.nodeGroupInstanceType");
// Role to access the cluster from using kubeconfig
// aws eks update-kubeconfig --name eks --region <region> --role-arn <role-arn>
const clusterAdmin = new iam.Role(this, eksClusterName+'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
// Cluster properties
const clusterProps = {
clusterName: current_env+eksClusterName,
// Default capacity as 0 denotes infinite number of worker nodes
// To avoid allocate the max number worker node while creating control plane
defaultCapacity: 0,
vpc: vpc,
mastersRole: clusterAdmin
}
// Create a new EKS cluster control plane
const cluster = new eks.Cluster(this, eksClusterName, clusterProps);
const eksOptimizedImage = {
//standard or GPU-optimized
nodeType: eks.NodeType.STANDARD
};
const nodeGroupMachineImage = new eks.EksOptimizedImage(eksOptimizedImage);
// defining autoscaling group for worker nodes which can be scalled up or down at any time
const rcAsg = new asg.AutoScalingGroup(this, current_env+'ASG', {
vpc: vpc,
instanceType: nodeGroupInstanceType,
machineImage: nodeGroupMachineImage,
// Create a keypair to ssh into the worker nodes and give the keypair here
// It should be same account and region
// keyName: nodeGroupKeyName,
minCapacity: nodeGroupMinCapacity,
maxCapacity: nodeGroupMaxCapacity,
desiredCapacity: nodeGroupDesiredCapacity,
updateType: asg.UpdateType.ROLLING_UPDATE,
vpcSubnets: {subnetType: ec2.SubnetType.PRIVATE}
});
cluster.addAutoScalingGroup(rcAsg, {
mapRole: true
})
}
}
一旦我运行它,得到如下输出模板。
{
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
// all resources
},
"Parameters": {
"AssetParametersea4957b1606259534983fnjdfs934r4b6ad19a204S3Bucket371D99F8": {
"Type": "String",
"Description": "S3 bucket for asset \"ea4957b1606m93439fmrefew99cc02944b6ad19a204\""
},
// more parameters.
}
}
如何避免这些资产参数?
解决方案
答案是您无法避免 CDK 应用程序中的资产参数。并非每个应用程序都有资产,只要有普通 cloudformation 无法完成的功能,它们就会引入这些资产参数,这些参数将在此期间使用CDK deploy
。
推荐阅读
- javascript - Javascript 数组变量在第二次使用时为空
- javascript - 在 C# 中将 (Javascript)TypedArray 字符串转换为 byteArray 的最佳方法
- c++ - C++ 汽车模拟
- hugo - 在以 Hugo Academic 为主题的网站中,如何在组织名称中添加换行符?
- git - 如何删除未跟踪的本地文件
- javascript - 如果屏幕上加载了确定的图像,如何自动单击单选按钮?
- php - 我在控制器中做错了什么导致 500 错误?
- javascript - 如何使阵列中的随机图像响应?
- r - 将命名行添加到 R 中的 data.frame
- javascript - 如何让我的 if 语句在我的游戏中发挥作用?