Home > Net >  AWS CDK - S3 to Lambda: creation of the S3EventSource with bucket returning `Property '_resourc
AWS CDK - S3 to Lambda: creation of the S3EventSource with bucket returning `Property '_resourc

Time:11-11

I am trying to create a cdk stack to trigger a lambda after a s3:ObjectCreated:Put event on S3.

I am following some tutorials (like this one), but I am not able to make the creation of the event source S3EventSource works, I am receiving a Typescript error when I am trying to add the bucket property to the event source:

const s3PutEventSource = new S3EventSource(bucket, {
  events: [
    s3.EventType.OBJECT_CREATED_PUT
  ]
});

Currently I am receiving the following error on my IDE and also when I try to run the npm run cdk deploy command:

lib/cdk-s3-stack.ts:29:48 - error TS2345: Argument of type 'import("/Users/jeprandini/cdk-s3/src/node_modules/@aws-cdk/aws-s3/lib/bucket").Bucket' is not assignable to parameter of type 'import("/Users/jeprandini/cdk-s3/src/node_modules/@aws-cdk/aws-ec2/node_modules/@aws-cdk/aws-s3/lib/bucket").Bucket'.
Property '_resource' is missing in type 'import("/Users/jeprandini/cdk-s3/src/node_modules/@aws-cdk/aws-s3/lib/bucket").Bucket' but required in type 'import("/Users/jeprandini/cdk-s3/src/node_modules/@aws-cdk/aws-ec2/node_modules/@aws-cdk/aws-s3/lib/bucket").Bucket'.

29     const s3PutEventSource = new S3EventSource(bucket, {
                                                ~~~~~~

node_modules/@aws-cdk/aws-ec2/node_modules/@aws-cdk/aws-s3/lib/bucket.d.ts:1489:22
    1489     private readonly _resource;
                            ~~~~~~~~~
    '_resource' is declared here.

Subprocess exited with error 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! s3-to-lambda-cdk@0.1.0 cdk: `cdk "deploy"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the s3-to-lambda-cdk@0.1.0 cdk script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jeprandini/.npm/_logs/2021-11-09T20_53_14_772Z-debug.log

My stack code:

import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { S3EventSource} from '@aws-cdk/aws-lambda-event-sources';

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

const BUCKET_NAME = 'demo-bucket'

// S3 bucket
const bucket = new s3.Bucket(this, BUCKET_NAME, {
  /**
   * The following properties ensure the bucket is properly 
   * deleted when we run cdk destroy */
  autoDeleteObjects: true,
  removalPolicy: cdk.RemovalPolicy.DESTROY
});

const lambdaReadStream = new NodejsFunction(this, 'readStream', {
  entry: 'lambda-fns/readStream/handler.js',
  handler: 'handler'
});


const s3eventSource = new S3EventSource(bucket, {
  events: [
    s3.EventType.OBJECT_CREATED_PUT
  ]
});

lambdaReadStream.addEventSource(s3eventSource);

// Outputs
new cdk.CfnOutput(this, 'BucketArn', { value: bucket.bucketArn });
new cdk.CfnOutput(this, 'LambdaFunctionArn', { value: lambdaReadStream.functionArn });
 }
}

my package.json file:

{
  "name": "cdk-s3",
  "version": "0.1.0",
  "bin": {
    "cdk-s3": "bin/cdk-s3.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-cdk/assert": "1.116.0",
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "aws-cdk": "^1.127.0",
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "ts-node": "^9.0.0",
    "typescript": "~3.9.7"
  },
  "dependencies": {
    "@aws-cdk/aws-lambda": "^1.95.1",
    "@aws-cdk/aws-s3": "1.95.1",
    "@aws-cdk/aws-lambda-event-sources": "^1.127.0",
    "@aws-cdk/aws-lambda-nodejs": "^1.127.0",
    "@aws-cdk/core": "^1.127.0"
  }
}

CodePudding user response:

Looks like you are having some dependencies conflicts in your cdk imports, for some dependencies you are using the version 1.127.0 and for others you are using the version 1.95.1 for example the @aws-cdk/aws-s3 library.

Normally you need to use the same version for all your cdk modules. Try to use the version ˆ1.95.1 for all of your CDK modules.

To to this, change the cdk dependencies to the version ˆ1.95.1, use the command rm -rf node_modules to delete your node_modules directory and run the npm i to recreate the node_modules dir and to install your modules again.

After that you able to run the command npm run cdk deploy without issues.

  • Related