无法从上下文提供者 ami 检索值,因为未在堆栈级别指定帐户/区域

问题描述 投票:0回答:2

您好,当我尝试在 AWS CDK 上创建堆栈时遇到问题

    import * as cdk from '@aws-cdk/core';
    import ec2 = require('@aws-cdk/aws-ec2');
    import { SubnetType } from '@aws-cdk/aws-ec2';

    export class FirstDemoStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        const natGatewayProvider = ec2.NatProvider.instance({
          instanceType: new ec2.InstanceType('t3.micro')
        });

        const vpc = new ec2.Vpc(this, 'VPC_Test', {
          cidr: '10.0.0.0/26',
          maxAzs: 1,
          subnetConfiguration:[
            {
              subnetType: ec2.SubnetType.PUBLIC,
              name: 'Ingress',
              cidrMask: 28
            },
            {
              cidrMask: 28,
              name: 'Application',
              subnetType: ec2.SubnetType.PRIVATE
            }
          ],
          natGatewayProvider,
          natGateways:2,
        });
      }
    }

所以我收到下一个错误

    Cannot retrieve value from context provider ami since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)

我知道我需要将区域和帐户添加到我的堆栈中,但我不知道在哪里

typescript amazon-web-services amazon-ec2 aws-cdk
2个回答
7
投票

如果我们仔细分析该错误,则表明为了找到适合您的 EC2 实例的 AMI,需要设置一个区域/帐户。有几种方法可以做到这一点。

1) 推荐 实例化堆栈时设置信息

在您的

index.ts
文件中,您将找到
new FirstDemoStack(...)
调用。您需要修改它以包含一些环境变量。您可以在此处设置您的帐户和区域。

new FirstDemoStack(app, 'first-demo-stack', { env: {
  account: 'your-account-number',
  region: 'us-east-1' // or whatever region you use
}});

2)您可以在环境变量中设置它。

您需要将

CDK_DEFAULT_ACCOUNT
CDK_DEFAULT_REGION
设置为其各自的值。您可以使用它,但是如果您需要使用两个区域或两个帐户,则需要对它们进行硬编码。


0
投票

您需要在堆栈级别定义环境变量,因此它看起来像这样:

import * as cdk from '@aws-cdk/core';
import ec2 = require('@aws-cdk/aws-ec2');
import { SubnetType } from '@aws-cdk/aws-ec2';

require("dotenv").config();

const config = {
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION,
  },
};

export class FirstDemoStack extends cdk.Stack {
      constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
        // Required to change here.
        // super(scope, id, props);
        super(scope, id, { ...props, env: config.env });

        const natGatewayProvider = ec2.NatProvider.instance({
          instanceType: new ec2.InstanceType('t3.micro')
        });

        const vpc = new ec2.Vpc(this, 'VPC_Test', {
          cidr: '10.0.0.0/26',
          maxAzs: 1,
          subnetConfiguration:[
            {
              subnetType: ec2.SubnetType.PUBLIC,
              name: 'Ingress',
              cidrMask: 28
            },
            {
              cidrMask: 28,
              name: 'Application',
              subnetType: ec2.SubnetType.PRIVATE
            }
          ],
          natGatewayProvider,
          natGateways:2,
        });
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.