简体   繁体   English

如何解决 CDK CLI 版本不匹配的问题

[英]How to solve CDK CLI version mismatch

I'm getting following error:我收到以下错误:

This CDK CLI is not compatible with the CDK library used by your application.此 CDK CLI 与您的应用程序使用的 CDK 库不兼容。 Please upgrade the CLI to the latest version.请将 CLI 升级到最新版本。 (Cloud assembly schema version mismatch: Maximum schema version supported is 8.0.0, but found 9.0.0) (云程序集架构版本不匹配:支持的最大架构版本是 8.0.0,但发现是 9.0.0)

after issuing cdk diff command.发出cdk diff命令后。

I did run npm install -g aws-cdk@latest after which I've successfully installed new versions of packages: Successfully installed aws-cdk.assets-1.92.0 aws-cdk.aws-apigateway-1.92.0 aws-cdk.aws-apigatewayv2-1.92.0... etc .我确实运行npm install -g aws-cdk@latest之后我成功安装了新版本的软件包: Successfully installed aws-cdk.assets-1.92.0 aws-cdk.aws-apigateway-1.92.0 aws-cdk.aws-apigatewayv2-1.92.0... etc with pip install -r requirements.txt使用pip install -r requirements.txt

However after typing cdk --version I'm still getting 1.85.0 (build 5f44668) .但是,在输入cdk --version后,我仍然得到1.85.0 (build 5f44668)

My part of setup.py is as follows:我的setup.py部分如下:

    install_requires=[
    "aws-cdk.core==1.92.0",
    "aws-cdk.aws-ec2==1.92.0",
    "aws-cdk.aws_ecs==1.92.0",
    "aws-cdk.aws_elasticloadbalancingv2==1.92.0"
],

And I'm stuck now, as downgrading packages setup.py to 1.85.0 throwing ImportError: cannot import name 'CapacityProviderStrategy' from 'aws_cdk.aws_ecs' .我现在被困住了,因为将包setup.py降级到 1.85.0 抛出ImportError: cannot import name 'CapacityProviderStrategy' from 'aws_cdk.aws_ecs'

Help:), I would like to use newest packages version.帮助:),我想使用最新的软件包版本。

So I've fixed it, but is too chaotic to describe the steps.所以我已经修复了它,但是描述步骤太混乱了。

It seems like there are problems with the symlink符号链接似乎有问题

/usr/local/bin/cdk

which was pointing to version 1.85.0 and not the one I updated to 1.92.0 .它指向版本1.85.0而不是我更新到1.92.0的版本。

I removed the aws-cdk from the node_modules and installed it again, then removed the symlink /usr/local/bin/cdk and recreated it manually with我从node_modules中删除了aws-cdk并再次安装它,然后删除了符号链接/usr/local/bin/cdk并使用手动重新创建它

ln -s /usr/lib/node_modules/aws-cdk/bin/cdk /usr/local/bin/cdk

I encountered this issue with a typescript package, after upgrading the cdk in package.json.在升级 package.Z466DEEC76ECDF24DCA564D 中的 cdk 后,我遇到了 typescript package 的这个问题。 As Maciej noted upgrading did not seem to work.正如 Maciej 所指出的,升级似乎不起作用。 I am installing the cdk cli with npm, and an uninstall followed by an install fixed the issue.我正在使用 npm 安装 cdk cli,卸载后安装解决了该问题。

npm -g uninstall aws-cdk
npm -g install aws-cdk

I have been experiencing this a few times as well, so i am just dropping this solution which helps me resolves version mismatches, particularly on existing projects.我也经历过几次,所以我只是放弃了这个解决方案,它可以帮助我解决版本不匹配问题,特别是在现有项目上。

For Python:对于 Python:

What you need to do is modify the setup.py to specify the latest version.您需要做的是修改 setup.py 以指定最新版本。

Either, implicitly要么,隐含地

    install_requires=[
    "aws-cdk.core",
    "aws-cdk.aws-ec2",
    "aws-cdk.aws_ecs",
    "aws-cdk.aws_elasticloadbalancingv2"
],

or explicitly;或明确地;

    install_requires=[
    "aws-cdk.core==1.xx.x",
    "aws-cdk.aws-ec2==1.xx.x",
    "aws-cdk.aws_ecs==1.xx.x",
    "aws-cdk.aws_elasticloadbalancingv2==1.xx.x"
],

Then from project root, run;然后从项目根目录运行;

setup.py install

For TypeScript:对于 TypeScript:

Modify package.json;修改package.json;

"dependencies": {
    "@aws-cdk/core" : "latest",
    "source-map-support": "^0.5.16"
  }

Then run from project root:然后从项目根目录运行:

npm install

I hope this helps.我希望这有帮助。 Please let me know if I need to elaborate or provide more details.如果我需要详细说明或提供更多详细信息,请告诉我。

nothing helps for mac OS except this command:除了以下命令,对 mac OS 没有任何帮助:

yarn global upgrade aws-cdk@latest yarn 全局升级 aws-cdk@latest

Uninstall the CDK version:卸载 CDK 版本:

npm uninstall -g aws-cdk

Install specfic version which your application is using.安装您的应用程序正在使用的特定版本。 For ex: CDK 1.158.0例如:CDK 1.158.0

npm install -g aws-cdk@1.158.0

For those coming here that are not using a global install (using cdk from node_modules ) and using a mono-repo.对于那些不使用全局安装(使用来自node_modules的 cdk)和使用单存储库的人来说。 This issue is due to the aws-cdk package of the devDependencies not matching the version of the dependencies for the package.此问题是由于 devDependencies 的aws-cdk package 与devDependencies的依赖项版本不匹配所致。

I was using "aws-cdk": "2.18.0" in my root package.json but all my packages were using "aws-cdk-lib": "2.32.1" as their dependencies.我在我的根package.json中使用"aws-cdk": "2.18.0"但我所有的包都使用"aws-cdk-lib": "2.32.1"作为它们的依赖项。 By updating the root package.json to use "aws-cdk": "2.31.1" this solved the issue.通过更新根package.json以使用"aws-cdk": "2.31.1"这解决了问题。

If you've made it down here there are 2 options to overcome this issue.如果你在这里成功了,有两个选项可以解决这个问题。

  1. Provision a cloud9 env and run the code there.配置一个 cloud9 环境并在那里运行代码。
  2. Run the app in a container.在容器中运行应用程序。

Move the cdk app into a folder so that you have a parent folder to put a dockerfile a makefile and a dockercompose file.将 cdk 应用程序移动到一个文件夹中,以便您有一个父文件夹来放置 dockerfile、makefile 和 dockercompose 文件。

fire up docker desktop cd into the root folder from the terminal and then run make启动 docker 桌面 cd 从终端进入根文件夹,然后运行make

Dockerfile contents Dockerfile 内容

FROM ubuntu:20.04 as compiler

WORKDIR /app/
RUN apt-get update -y \
    && apt install python3 -y \
    && apt install python3-pip -y \
    && apt install python3-venv -y \
    && python3 -m venv venv


ARG NODE_VERSION=16

RUN ls

RUN apt-get update
RUN apt-get install xz-utils
RUN apt-get -y install curl

RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install -y && \
    curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - && \
    apt install -y nodejs

RUN apt-get install -y python-is-python3
RUN npm install -g aws-cdk
RUN python -m venv /opt/venv

docker-compose.yaml contents docker-compose.yaml 内容

version: '3.6'
services:
  cdk-base:
    build: .
    image: cdk-base
    command: ${COMPOSE_COMMAND:-bash}
    volumes:
      - .:/app
      - /var/run/docker.sock:/var/run/docker.sock #Needed so a docker container can be run from inside a docker container
      - ~/.aws/:/root/.aws:ro

Makefile content Makefile内容

SHELL=/bin/bash
CDK_DIR=cdk_app/
COMPOSE_RUN = docker-compose run --rm cdk-base
COMPOSE_UP = docker-compose up
PROFILE = --profile default

all: pre-reqs synth
pre-reqs: _prep-cache container-build npm-install 
    

_prep-cache: #This resolves Error: EACCES: permission denied, open 'cdk.out/tree.json'
    mkdir -p cdk_websocket/cdk.out/

container-build: pre-reqs
    docker-compose build

container-info:
    ${COMPOSE_RUN} make _container-info

_container-info:
    ./containerInfo.sh

clear-cache:
    ${COMPOSE_RUN} rm -rf ${CDK_DIR}cdk.out && rm -rf ${CDK_DIR}node_modules

cli: _prep-cache
    docker-compose run cdk-base /bin/bash

npm-install: _prep-cache
    ${COMPOSE_RUN} make _npm-install

_npm-install:
    cd ${CDK_DIR} && ls && python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt && npm -v && python --version

npm-update: _prep-cache
    ${COMPOSE_RUN} make _npm-update

_npm-update:
    cd ${CDK_DIR} && npm update

synth: _prep-cache
    ${COMPOSE_RUN} make _synth

_synth:
    cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk synth --no-staging ${PROFILE} && cdk deploy --require-approval never ${PROFILE}

bootstrap: _prep-cache
    ${COMPOSE_RUN} make _bootstrap

_bootstrap:
    cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk bootstrap ${PROFILE}

deploy: _prep-cache
    ${COMPOSE_RUN} make _deploy

_deploy: 
    cd ${CDK_DIR} && cdk deploy --require-approval never ${PROFILE}

destroy:
    ${COMPOSE_RUN} make _destroy

_destroy:
    cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk destroy --force ${PROFILE}

diff: _prep-cache
    ${COMPOSE_RUN} make _diff

_diff: _prep-cache
    cd ${CDK_DIR} && cdk diff ${PROFILE}

test: 
    ${COMPOSE_RUN} make _test

_test: 
    cd ${CDK_DIR} && npm test 

This worked for me in my dev environment since I had re-cloned the source, I needed to re-run the npm install command.这在我的开发环境中对我有用,因为我重新克隆了源代码,我需要重新运行npm install命令。 A sample package.json might look like this (update the versions as needed):示例 package.json 可能如下所示(根据需要更新版本):

{
    "dependencies": {
        "aws-cdk": "2.27.0",
        "node": "^16.14.0"
    }
}

I changed typescript": "^4.7.3" version and it worked. Note: cdk version 2("aws-cdk-lib": "^2.55.0")我更改了 typescript": "^4.7.3" 版本并且有效。注意:cdk version 2("aws-cdk-lib": "^2.55.0")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM