我正在处理一个问题,在 stackoverflow 上发布了一些其他类似的解决方案
(1) “this”类型的参数不可分配给 AWS CDK 中“Construct”类型的参数
(2) AWS CDK, typescript - 'this' 类型的参数不可分配给 'Construct' 类型的参数
我尝试使我所有的aws库版本匹配按照这个建议:
“这可能是因为您使用的 CDK 模块与 CDK 核心库的版本不同。CDK 更新非常频繁,所以这是一个很常见的错误。
要解决此问题,您需要将所有 cdk 包更新到同一版本。”
建议在这里用不同的词重复,所以我做了所有这些:
Delete node_modules folder
Delete package-lock.json
Ensure all dependencies in package.json are using same version.
Remove carrot ^ symbol before dependencies
npm install
现在我的包json是这样的:
{
"name": "cdk-eb-infra",
"version": "0.1.0",
"bin": {
"cdk-eb-infra": "bin/cdk-eb-infra.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@types/jest": "^29.2.4",
"@types/node": "18.11.15",
"aws-cdk": "2.59.0",
"jest": "^29.3.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "~4.9.4"
},
"dependencies": {
"@aws-cdk/aws-elasticbeanstalk": "1.187.0", // note identical versions
"@aws-cdk/aws-iam": "1.187.0", // note identical versions
"@aws-cdk/aws-s3-assets": "1.187.0", // note identical versions
"@aws-cdk/core": "1.187.0", // note identical versions
"aws-cdk-lib": "2.59.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
}
现在我尝试运行
cdk deploy
然后我得到
lib/cdk-eb-infra-stack.ts:20:57 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'CdkEbInfraStack' is missing the following properties from type 'Construct': onValidate, onPrepare, onSynthesize, validate, and 2 more.
20 const app = new elasticbeanstalk.CfnApplication(this, "Application", {
~~~~
lib/cdk-eb-infra-stack.ts:25:76 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
25 const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, "AppVersion", {
~~~~
lib/cdk-eb-infra-stack.ts:37:37 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
37 const myRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
~~~~
lib/cdk-eb-infra-stack.ts:46:60 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
46 const instanceProfile = new iam.CfnInstanceProfile(this, myProfileName, {
解决方案是什么?
编辑:在
cdk-eb-infra-stack.ts
文件夹中分享我的/lib
文件的(抱歉,长)代码:
import * as cdk from "aws-cdk-lib";
import iam = require("@aws-cdk/aws-iam");
import elasticbeanstalk = require("@aws-cdk/aws-elasticbeanstalk");
import s3assets = require("@aws-cdk/aws-s3-assets");
import { Construct } from "constructs";
// import * as sqs from 'aws-cdk-lib/aws-sqs';
export class CdkEbInfraStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// Construct an S3 asset from the ZIP located from directory up.
const webAppZipArchive = new s3assets.Asset(this, "WebAppZip", {
path: `${__dirname}/../app.zip`,
});
// Create a ElasticBeanStalk app.
const appName = "MyWebApp";
const app = new elasticbeanstalk.CfnApplication(this, "Application", {
applicationName: appName,
});
// Create an app version from the S3 asset defined earlier
const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, "AppVersion", {
applicationName: appName,
sourceBundle: {
s3Bucket: webAppZipArchive.s3BucketName,
s3Key: webAppZipArchive.s3ObjectKey,
},
});
// Make sure that Elastic Beanstalk app exists before creating an app version
appVersionProps.addDependsOn(app);
// Create role and instance profile
const myRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
assumedBy: new iam.ServicePrincipal("ec2.amazonaws.com"),
});
const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName("AWSElasticBeanstalkWebTier");
myRole.addManagedPolicy(managedPolicy);
const myProfileName = `${appName}-InstanceProfile`;
const instanceProfile = new iam.CfnInstanceProfile(this, myProfileName, {
instanceProfileName: myProfileName,
roles: [myRole.roleName],
});
}
}
我完全复制粘贴了 模块 2:使用 AWS CDK 创建基础设施
看来您正在混合使用 CDKv1 和 CDKv2 软件包。
这将属于
Ensure all dependencies in package.json are using same version.
改变
"dependencies": {
"@aws-cdk/aws-elasticbeanstalk": "1.187.0", // note identical versions
"@aws-cdk/aws-iam": "1.187.0", // note identical versions
"@aws-cdk/aws-s3-assets": "1.187.0", // note identical versions
"@aws-cdk/core": "1.187.0", // note identical versions
"aws-cdk-lib": "2.59.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
到
"dependencies": {
"aws-cdk-lib": "2.59.0", // CDKv2
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
编辑: 根据@fedonev 的回答
像这样更新你的导入:
import {
aws_iam as iam,
aws_elasticbeanstalk as elasticbeanstalk,
aws_s3_assets as s3_assets,
} from "aws-cdk-lib";
在 V2 中,只有不稳定的“alpha”模块在单独的包中。
对不起,我是 StackOverflow 上引用的新手。编辑是否有更好的方法来归因@fedonev
您正在混合 V1 和 V2 依赖项。删除 V1 依赖项,即您的
package.json
中版本为 1.187.0
的包。从 aws-cdk-lib
导入特定于服务的包:
import {
aws_iam as iam,
aws_elasticbeanstalk as elasticbeanstalk,
aws_s3_assets as s3_assets,
} from "aws-cdk-lib";
在 V2 中,只有不稳定的“alpha”模块在单独的包中:
包装 | CDK版本 | 构造 |
---|---|---|
|
v2 | L1 ( ) 和稳定的 L2/L3 构造 |
|
v2 | 实验性 L2/L3 结构 |
|
v1 | 全部 |
Fedonev 和 Jacob Greenbow 都发布了有用的回复。我能够进行以下更改并使其正常工作:
import { Stack, StackProps, aws_iam as iam, aws_elasticbeanstalk as elasticbeanstalk, aws_s3_assets as s3_assets } from "aws-cdk-lib";
import { Construct } from "constructs";
// import * as sqs from 'aws-cdk-lib/aws-sqs';
export class CdkEbInfraStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// Construct an S3 asset from the ZIP located from directory up.
const webAppZipArchive = new s3_assets.Asset(this, "WebAppZip", {
path: `${__dirname}/../app.zip`,
});
// Create a ElasticBeanStalk app.
const appName = "MyWebApp";
const app = new elasticbeanstalk.CfnApplication(this, "Application", {
applicationName: appName,
});
// Create an app version from the S3 asset defined earlier
const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, "AppVersion", {
applicationName: appName,
sourceBundle: {
s3Bucket: webAppZipArchive.s3BucketName,
s3Key: webAppZipArchive.s3ObjectKey,
},
});
// Make sure that Elastic Beanstalk app exists before creating an app version
appVersionProps.addDependsOn(app);
// Create role and instance profile
const myRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
assumedBy: new iam.ServicePrincipal("ec2.amazonaws.com"),
});
const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName("AWSElasticBeanstalkWebTier");
myRole.addManagedPolicy(managedPolicy);
const myProfileName = `${appName}-InstanceProfile`;
const instanceProfile = new iam.CfnInstanceProfile(this, myProfileName, {
instanceProfileName: myProfileName,
roles: [myRole.roleName],
});
}
}
请注意,
Stack
和 StackProps
也必须导入,以避免破坏亚马逊的其他示例。