如何避免CDK合成器参数化云形成

时间:2019-12-09 10:04:53

标签: amazon-web-services amazon-cloudformation aws-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();

lib / 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.
  }
}

如何避免这些资产参数?

1 个答案:

答案 0 :(得分:0)

答案是您无法避免CDK应用程序中的资产参数。并非每个应用程序都有资产,每当有功能无法通过纯云形式完成时,它们就会引入这些资产参数,这些参数将在CDK deploy时间内使用。

参考-https://github.com/aws/aws-cdk/issues/5403