0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CDKでもLambdaのコンテナ対応されていた

Posted at

はじめに

タイトルの通りです。re:invent2020にてLambdaのコンテナサポートが発表されました。
しかしCDKの対応はもう少し先かとおもっていたのですが、どうやらドキュメントよりライブラリがアップデートされていました。

実装してみる

今回はLambdaのみの実装です。とりあえずデプロイがされてテスト実行が成功すればOKということにします。

Dockerfileと関数は本当に最小限です。

Dockerfile
FROM public.ecr.aws/lambda/nodejs:12

COPY index.js ./

CMD ["index.handler"]
index.ts
export const handler = () => {
  console.log('Hello lambda container')
  return { statusCode: 200 }
}

後はCDKを実装します。

lib/lambda-container-stack.ts
import * as cdk from '@aws-cdk/core'
import * as lambda from '@aws-cdk/aws-lambda'

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

    new lambda.DockerImageFunction(this, 'Function', {
      code: lambda.DockerImageCode.fromImageAsset('./lambda'),
    })    
  }
}

なんとこれでcdk deployを実行するとDockerビルドとECRへのプッシュを勝手にやってくれるらしいのです。これは楽。
とりあえず結果としてテスト実行するとconsole.log表示ができたので成功かと思います。

Hello lambda container

おわりに

かなり短いですが終わりです。ソースはこちらで公開しています。これでLambdaの夢が広がりますね。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?