How to use AWS CDK to compile and deploy a typescript api with dependencies to lambda?
I have an api in TypeScript that i would like to deploy to a Lambda function. The project has dependencies that needs to be compiled on the platform it's running on, so I need a build step that can do the build on Linux machine in aws before it deploys.
How can I leverage the CDK, codepipeline (or codebuild?) to do this?
Following the [workshop](https://cdkworkshop.com/20-typescript.html) its super easy to get going with the pipeline that can deploy a "hello world" lambda, but I'm struggling to advance with this.
How can I compile the TS code as a step in the pipeline, or how can set up a step in the pipeline that pulls code from another repo and then compiles and deploys it?
I've tried to use the aws-cdk-lib/aws-lambda-nodejs constructor that claims to compile automatically. But, so far the pipelines crashes on the 2. step (build step in the pipeline self mutate process) because it can't find the dependencies in the Lamba.
/lambda
- package-lock.json
- package.json
- graph.js:
```
import { ApolloServer, gql } from 'apollo-server-lambda'
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core'
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground]
});
exports.handler = server.createHandler();
```
/lib:
- pipeline-stack
```
...imports...
export class PipelineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const pipeline = new CodePipeline(this, '*ID', {
pipelineName: '*NAME',
synth: new CodeBuildStep('*ID', {
input: CodePipelineSource.connection('*ghaccount/ghrepo', '*branch', {
connectionArn: "*GHCONNECTION ARN"
}),
installCommands: ['npm install -g aws-cdk-lib'],
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
]
})
})
pipeline.addStage(new PipelineStage(this, 'Deploy', {
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }
}))
}
}
```
- pipeline-stage.ts
-
```
import * as cdk from 'aws-cdk-lib';
import { Construct } from "constructs";
import { LambdaStack } from './server-stack';
export class PipelineStage extends cdk.Stage {
constructor(scope: Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);
// services / resources the app needs to run goes here ->>
const lambdaStack = new LambdaStack(this, 'LambdaStack');
}
}
```
- server-stack.ts
```
export class LambdaStack extends cdk.Stack {
public readonly api: apiGateway.RestApi
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
const graphqlLambda = new NodeLambda.NodejsFunction(this, 'adeegso-api-lambda', {
entry: path.join(__dirname, '/../lambda/graph.ts'),
handler: 'graph.handler',
depsLockFilePath: "lambda/package-lock.json",
})
this.api = new apiGateway.LambdaRestApi(this, 'adeegso-api-endpoint', {
handler: graphqlLambda,
})
}
}
```
Thanks in advance for any help provided!