![](/img/trans.png)
[英]Argument of type 'this' is not assignable to parameter of type 'Construct' in AWS CDK
[英]AWS CDK says Argument of type 'this' is not assignable to parameter of type 'Construct'
我正在處理一個問題,該問題在 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
現在我的 package 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, {
解決辦法是什么?
編輯:在/lib
文件夾中共享我的cdk-eb-infra-stack.ts
文件的(抱歉,長)代碼:
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”模塊在單獨的包中。
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
也必須導入以避免破壞亞馬遜示例的 rest。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.