Giter Site home page Giter Site logo

Comments (5)

hawflau avatar hawflau commented on July 24, 2024

Hey @Schwar999, thanks for raising the issue.

To help us reproduce the issue, can you please share your Lambda Function Construct in CDK and corresponding Lambda Function Resource in synth-ed template?

from aws-sam-cli.

Schwar999 avatar Schwar999 commented on July 24, 2024

@hawflau
Thank you for your reply.

Here are some of my codes.
Would you be able to analyze them?

Synth-ed template

"func0915CAEC": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Architectures": [
     "x86_64"
    ],
    "Code": {
     "S3Bucket": "cdk-xxxx",
     "S3Key": "xxxx.zip"
    },
    "Environment": {
     "Variables": {
      "ENV_NAME": "dev",
      "LOG_LEVEL": "debug",
      "AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
     }
    },
    "Handler": "index.handler",
    "Layers": [
     "arn:aws:lambda:ap-northeast-1:580247275435:layer:LambdaInsightsExtension:78"
    ],
    "MemorySize": 256,
    "Role": {
     "Fn::GetAtt": [
      "funcServiceRoleDA6E3B24",
      "Arn"
     ]
    },
    "Runtime": "nodejs18.x",
    "VpcConfig": {
     "SecurityGroupIds": [
      {
       "Fn::GetAtt": [
        "funcSecurityGroup8BD4105F",
        "GroupId"
       ]
      }
     ],
     "SubnetIds": [
      "subnet-000",
      "subnet-001"
     ]
    }
   },
   "DependsOn": [
    "funcServiceRoleDefaultPolicy2124A06A",
    "funcServiceRoleDA6E3B24"
   ],
   "Metadata": {
    "aws:cdk:path": "test-stack/func/Resource",
    "aws:asset:path": "asset.71bec7b1e16c2b00a998c4abec8abb8ebcf9242a7b",
    "aws:asset:is-bundled": true,
    "aws:asset:property": "Code"
   }
  }

stack.ts

import { Duration, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Topic } from "aws-cdk-lib/aws-sns";
import { SqsSubscription } from "aws-cdk-lib/aws-sns-subscriptions";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Architecture, LambdaInsightsVersion, Runtime, Tracing } from "aws-cdk-lib/aws-lambda";
import { Config } from "./config";
import { SubnetType, Vpc } from "aws-cdk-lib/aws-ec2";
import { Queue, QueuePolicy } from "aws-cdk-lib/aws-sqs";
import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
import { RetentionDays } from "aws-cdk-lib/aws-logs";
import { Effect, PolicyStatement, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { QueueProps } from "aws-cdk-lib/aws-sqs/lib/queue";

export class TestStack extends Stack {
  constructor(scope: Construct, id: string, props: TestStackProps, config: Config) {
    super(scope, id, props);

    const vpc = Vpc.fromLookup(this, "test-vpc", { vpcId: config.testVpcId });

    const funcLambda = new NodejsFunction(this, `func`, {
      memorySize: 256,
      vpc: vpc,
      vpcSubnets: vpc.selectSubnets({ subnetType: config.testVpcSubnetType as SubnetType }),
      allowPublicSubnet: config.testVpcSubnetType === "Public",
      runtime: Runtime.NODEJS_18_X,
      timeout: Duration.seconds(config.pushNotifier.func.lambdaAndQueueVisibilityTimeoutSeconds),
      environment: {
        ENV_NAME: config.env,
        LOG_LEVEL: config.logLevel,
      },
      architecture: Architecture.X86_64,
      insightsVersion: LambdaInsightsVersion.fromInsightVersionArn(config.lambdaInsightsArn),
      logRetention: RetentionDays.ONE_YEAR,
      tracing: Tracing.ACTIVE,
    });

lambda.ts

import { Context, SQSEvent, SQSRecord } from "aws-lambda";
import got from "got";
import { defaultLogger, getLogger } from "./logger";
import { captureLambdaHandler, Tracer } from "@aws-lambda-powertools/tracer";
import middy from "@middy/core";

let logger = defaultLogger;

export const handler = middy(async (event: SQSEvent, context: Context) => {
  logger = getLogger(context);
  const sqsRecord: SQSRecord = event.Records[0];
  const sqsBody: TestMessage = JSON.parse(sqsRecord.body);
  return notifySingle(sqsRecord.messageId, sqsBody.endpoint, sqsBody.payload);
}).use(captureLambdaHandler(tracer));

from aws-sam-cli.

mndeveci avatar mndeveci commented on July 24, 2024

I've tried to reproduce this issue however I was able to invoke the function locally.

Here is the folder structure;

.
├── README.md
├── bin
│   └── cdk-local.ts
├── cdk.json
├── jest.config.js
├── lib
│   ├── cdk-local-stack.func.ts
│   └── cdk-local-stack.ts
├── package-lock.json
├── package.json
└── tsconfig.json

Here are the contents of the files;
bin/cdk-local.ts

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { CdkLocalStack } from '../lib/cdk-local-stack';

const app = new cdk.App();
new CdkLocalStack(app, 'CdkLocalStack', {});

lib/cdk-local-stack.ts

export interface Response {
    result: number;
}

export const handler = async (event: {}): Promise<Response> => {
    const result = {
        result: 10,
    };

    return result;
};

lib/cdk-local-stack.func.ts

import * as cdk from 'aws-cdk-lib';
import { Architecture, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Construct } from 'constructs';

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

    // The code that defines your stack goes here

    const funcLambda = new NodejsFunction(this, `func`, {
      memorySize: 256,
      runtime: Runtime.NODEJS_18_X,
      architecture: Architecture.X86_64,
      logRetention: RetentionDays.ONE_YEAR,
      tracing: Tracing.ACTIVE,
    });
  }
}

I've used your tsconfig.json file. I am running cdk synth and then sam local invoke --template cdk.out/CdkLocalStack.template.json func and I can invoke function locally.

Can you provide a reproducible example?

from aws-sam-cli.

lucashuy avatar lucashuy commented on July 24, 2024

Closing the issue, please reopen or create a new issue if you encounter any other problems.

from aws-sam-cli.

github-actions avatar github-actions commented on July 24, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

from aws-sam-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.