Hi Guys, I'm wonder if anyone has used the API gat...
# aws
f
Hi Guys, I'm wonder if anyone has used the API gateway with sending message to SQS before. It ran fine with pulumi up but when I'm trying to test the API it is giving me a weird message indicating it can't load module "@pulumi/aws/sqs/index.js" Here is how I setup this:
Copy code
import * as awsx from '@pulumi/awsx';
import { Context, nameOf } from './naming';
import * as sqs from '@pulumi/aws/sqs';
import * as sqssdk from 'aws-sdk/clients/sqs';

export function createApiGetaway(
	purpose: string,
	context: Context,
	queueName: string
): awsx.apigateway.API {
	const name = nameOf('api', context, purpose);

	const handler = async (event: any) => {
		const requestBody = event.body as string;

		const queue = await sqs.getQueue(
			{
				name: queueName,
			},
			{ async: true }
		);
		const sqsClient = new sqssdk();
		sqsClient.sendMessage(
			{
				QueueUrl: queue.url,
				MessageBody: requestBody,
			},
			() => {}
		);

		return {
			statusCode: 200,
			body: 'message',
		};
	};

	const api = new awsx.apigateway.API(name, {
		routes: [
			{
				path: '/',
				method: 'POST',
				eventHandler: handler,
			},
		],
	});

	return api;
}
The logs from testing this in the aws console is
Copy code
Execution log for request b8ae63fa-7d2b-427e-b629-6b3e0a702610
Tue Aug 25 21:59:08 UTC 2020 : Starting execution for request: b8ae63fa-7d2b-427e-b629-6b3e0a702610
Tue Aug 25 21:59:08 UTC 2020 : HTTP Method: POST, Resource Path: /
Tue Aug 25 21:59:08 UTC 2020 : Method request path: {}
Tue Aug 25 21:59:08 UTC 2020 : Method request query string: {}
Tue Aug 25 21:59:08 UTC 2020 : Method request headers: {}
Tue Aug 25 21:59:08 UTC 2020 : Method request body before transformations: {
    "sample":""
}
Tue Aug 25 21:59:08 UTC 2020 : Endpoint request URI: <https://lambda.eu-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-1:606148221596:function:ta-dev-api-damavand-process-image980655da-731571a/invocations>
Tue Aug 25 21:59:08 UTC 2020 : Endpoint request headers: {x-amzn-lambda-integration-tag=b8ae63fa-7d2b-427e-b629-6b3e0a702610, Authorization=************************************************************************************************************************************************************************************************************************************************************************************************************************23b3a8, X-Amz-Date=20200825T215908Z, x-amzn-apigateway-api-id=jxv808o1mk, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-1:606148221596:jxv808o1mk/test-invoke-stage/POST/, Accept=application/json, User-Agent=AmazonAPIGateway_jxv808o1mk, X-Amz-Security-Token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [TRUNCATED]
Tue Aug 25 21:59:08 UTC 2020 : Endpoint request body after transformations: {"resource":"/","path":"/","httpMethod":"POST","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"resourceId":"72qfdqq0qf","resourcePath":"/","httpMethod":"POST","extendedRequestId":"R2Jy_H3iDoEFdfw=","requestTime":"25/Aug/2020:21:59:08 +0000","path":"/","accountId":"606148221596","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1598392748608,"requestId":"b8ae63fa-7d2b-427e-b629-6b3e0a702610","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::606148221596:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.829 Linux/4.9.217-0.1.ac.205.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.262-b10 java/1.8.0_262 vendor/Oracle_Corporation","accountId [TRUNCATED]
Tue Aug 25 21:59:08 UTC 2020 : Sending request to <https://lambda.eu-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-1:606148221596:function:ta-dev-api-damavand-process-image980655da-731571a/invocations>
Tue Aug 25 21:59:08 UTC 2020 : Received response. Status: 200, Integration latency: 299 ms
Tue Aug 25 21:59:08 UTC 2020 : Endpoint response headers: {Date=Tue, 25 Aug 2020 21:59:08 GMT, Content-Type=application/json, Content-Length=781, Connection=keep-alive, x-amzn-RequestId=bc3f7c83-81d9-4b69-aebd-52e2fc0182d0, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5f4589ac-0d5733967208c879ec0782d5;sampled=0}
Tue Aug 25 21:59:08 UTC 2020 : Endpoint response body before transformations: {"errorType":"Error","errorMessage":"Cannot find module '@pulumi/aws/sqs/index.js'\nRequire stack:\n- /var/task/__index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","trace":["Error: Cannot find module '@pulumi/aws/sqs/index.js'","Require stack:","- /var/task/__index.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)","    at Function.Module._load (internal/modules/cjs/loader.js:842:27)","    at Module.require (internal/modules/cjs/loader.js:1026:19)","    at require (internal/modules/cjs/helpers.js:72:18)","    at /var/task/__index.js:23:34","    at Runtime.__f0 [as handler] (/var/task/__index.js:42:6)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
Tue Aug 25 21:59:08 UTC 2020 : Lambda execution failed with status 200 due to customer function error: Cannot find module '@pulumi/aws/sqs/index.js'
Require stack:
- /var/task/__index.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js. Lambda request id: bc3f7c83-81d9-4b69-aebd-52e2fc0182d0
Tue Aug 25 21:59:08 UTC 2020 : Method completed with status: 502
l
Looks like your event handler is trying to use Pulumi. That doesn't work. What's it trying to do?
n
yeah i agree, this is for TerraForm, not code… that’s probably why
pulumi up
worked and this doesn’t… if you want to use sqs in the function, you should look at
aws-sdk
or something?
f
Sorry guys for the delay. I'm trying to put a message in sqs queue from api gateway (event handler)
n
yeah you don’t need pulumi in the lambda (event handlers), you only need
aws-sdk