Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

チュートリアル: サーバーレス Hello World アプリケーションの作成

フォーカスモード
チュートリアル: サーバーレス Hello World アプリケーションの作成 - AWS Cloud Development Kit (AWS CDK) v2

これは v2 AWS CDK デベロッパーガイドです。旧版の CDK v1 は 2022 年 6 月 1 日にメンテナンスを開始し、2023 年 6 月 1 日にサポートを終了しました。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

これは v2 AWS CDK デベロッパーガイドです。旧版の CDK v1 は 2022 年 6 月 1 日にメンテナンスを開始し、2023 年 6 月 1 日にサポートを終了しました。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

このチュートリアルでは、 を使用して、以下で構成される基本的な API バックエンドを実装するシンプルなサーバーレスHello Worldアプリケーション AWS Cloud Development Kit (AWS CDK) を作成します。

  • Amazon API Gateway REST API – HTTP GET 要求を通じて関数を呼び出すために使用される HTTP エンドポイントを提供します。

  • AWS Lambda 関数 – HTTPエンドポイントで呼び出されたときにHello World!メッセージを返します。

  • 統合とアクセス許可 – リソースが相互にやり取りしてアクション (Amazon CloudWatch にログを書き込むなど) を実行するための設定の詳細およびアクセス許可。

以下の図は、このアプリケーションのコンポーネントを示しています。

API Gateway エンドポイントに GET 要求を送信するときに呼び出される Lambda 関数の図。

このチュートリアルでは、次の手順でアプリケーションを作成して操作します。

  1. AWS CDK プロジェクトを作成します。

  2. コンストラクトライブラリの L2 コンストラクトを使用して Lambda 関数と API Gateway REST API AWS を定義します。

  3. アプリケーションを にデプロイします AWS クラウド。

  4. でアプリケーションを操作します AWS クラウド。

  5. AWS クラウドからサンプルアプリケーションを削除します。

前提条件

このチュートリアルをスタートする前に、以下を完了してください。

  • を作成し AWS アカウント 、 AWS Command Line Interface (AWS CLI) をインストールして設定します。

  • Node.js および npm をインストールします。

  • npm install -g aws-cdk を使用して CDK Toolkit をグローバルにインストールします。

詳細については、「の開始方法 AWS CDK」を参照してください。

次の内容の基礎的な理解を持つことをお勧めします。

ステップ 1: プロジェクトの作成

このステップでは、 cdk init コマンドを使用して新しい CDK AWS CDK CLIプロジェクトを作成します。

CDK プロジェクトを作成する方法
  1. 選択した開始ディレクトリから、パソコン上で cdk-hello-world という名前のプロジェクトディレクトリを作成して移動します。

    $ mkdir cdk-hello-world && cd cdk-hello-world
  2. cdk init コマンドを使用し、任意のプログラミング言語で新しいプロジェクトを作成します。

    TypeScript
    $ cdk init --language typescript

    AWS CDK ライブラリをインストールします。

    $ npm install aws-cdk-lib constructs
    JavaScript
    $ cdk init --language javascript

    AWS CDK ライブラリをインストールします。

    $ npm install aws-cdk-lib constructs
    Python
    $ cdk init --language python

    仮想環境のアクティブ化

    $ source .venv/bin/activate # On Windows, run '.\venv\Scripts\activate' instead

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    (.venv)$ python3 -m pip install -r requirements.txt
    Java
    $ cdk init --language java

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    $ mvn package
    C#
    $ cdk init --language csharp

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    $ dotnet restore src
    Go
    $ cdk init --language go

    プロジェクトの依存関係をインストールします。

    $ go get github.com/aws/aws-cdk-go/awscdk/v2 $ go get github.com/aws/aws-cdk-go/awscdk/v2/awslambda $ go get github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway $ go mod tidy
    $ cdk init --language typescript

    AWS CDK ライブラリをインストールします。

    $ npm install aws-cdk-lib constructs

    CDK CLI は、次の構造でプロジェクトを作成します。

    TypeScript
    cdk-hello-world ├── .git ├── .gitignore ├── .npmignore ├── README.md ├── bin │ └── cdk-hello-world.ts ├── cdk.json ├── jest.config.js ├── lib │ └── cdk-hello-world-stack.ts ├── node_modules ├── package-lock.json ├── package.json ├── test │ └── cdk-hello-world.test.ts └── tsconfig.json
    JavaScript
    cdk-hello-world ├── .git ├── .gitignore ├── .npmignore ├── README.md ├── bin │ └── cdk-hello-world.js ├── cdk.json ├── jest.config.js ├── lib │ └── cdk-hello-world-stack.js ├── node_modules ├── package-lock.json ├── package.json └── test └── cdk-hello-world.test.js
    Python
    cdk-hello-world ├── .git ├── .gitignore ├── .venv ├── README.md ├── app.py ├── cdk.json ├── cdk_hello_world │ ├── __init__.py │ └── cdk_hello_world_stack.py ├── requirements-dev.txt ├── requirements.txt ├── source.bat └── tests
    Java
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk.json ├── pom.xml ├── src │ ├── main │ │ └── java │ │ └── com │ │ └── myorg │ │ ├── CdkHelloWorldApp.java │ │ └── CdkHelloWorldStack.java └── target
    C#
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk.json └── src ├── CdkHelloWorld │ ├── CdkHelloWorld.csproj │ ├── CdkHelloWorldStack.cs │ ├── GlobalSuppressions.cs │ └── Program.cs └── CdkHelloWorld.sln
    Go
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk-hello-world.go ├── cdk-hello-world_test.go ├── cdk.json ├── go.mod └── go.sum
    cdk-hello-world ├── .git ├── .gitignore ├── .npmignore ├── README.md ├── bin │ └── cdk-hello-world.ts ├── cdk.json ├── jest.config.js ├── lib │ └── cdk-hello-world-stack.ts ├── node_modules ├── package-lock.json ├── package.json ├── test │ └── cdk-hello-world.test.ts └── tsconfig.json

CDK CLI は、1 つのスタックを含む CDK アプリを自動的に作成します。CDK アプリインスタンスは App クラスから作成されます。次の内容は、CDK アプリケーションファイルの一部です。

TypeScript

bin/cdk-hello-world.ts にあります

#!/usr/bin/env node import 'source-map-support/register'; import * as cdk from 'aws-cdk-lib'; import { CdkHelloWorldStack } from '../lib/cdk-hello-world-stack'; const app = new cdk.App(); new CdkHelloWorldStack(app, 'CdkHelloWorldStack', { });
JavaScript

bin/cdk-hello-world.js にあります

#!/usr/bin/env node const cdk = require('aws-cdk-lib'); const { CdkHelloWorldStack } = require('../lib/cdk-hello-world-stack'); const app = new cdk.App(); new CdkHelloWorldStack(app, 'CdkHelloWorldStack', { });
Python

app.py にあります

#!/usr/bin/env python3 import os import aws_cdk as cdk from cdk_hello_world.cdk_hello_world_stack import CdkHelloWorldStack app = cdk.App() CdkHelloWorldStack(app, "CdkHelloWorldStack",) app.synth()
Java

src/main/java/.../CdkHelloWorldApp.java にあります

package com.myorg; import software.amazon.awscdk.App; import software.amazon.awscdk.Environment; import software.amazon.awscdk.StackProps; import java.util.Arrays; public class JavaApp { public static void main(final String[] args) { App app = new App(); new JavaStack(app, "JavaStack", StackProps.builder() .build()); app.synth(); } }
C#

src/CdkHelloWorld/Program.cs にあります

using Amazon.CDK; using System; using System.Collections.Generic; using System.Linq; namespace CdkHelloWorld { sealed class Program { public static void Main(string[] args) { var app = new App(); new CdkHelloWorldStack(app, "CdkHelloWorldStack", new StackProps { }); app.Synth(); } } }
Go

cdk-hello-world.go にあります

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) // ... func main() { defer jsii.Close() app := awscdk.NewApp(nil) NewCdkHelloWorldStack(app, "CdkHelloWorldStack", &CdkHelloWorldStackProps{ awscdk.StackProps{ Env: env(), }, }) app.Synth(nil) } func env() *awscdk.Environment { return nil }

bin/cdk-hello-world.ts にあります

#!/usr/bin/env node import 'source-map-support/register'; import * as cdk from 'aws-cdk-lib'; import { CdkHelloWorldStack } from '../lib/cdk-hello-world-stack'; const app = new cdk.App(); new CdkHelloWorldStack(app, 'CdkHelloWorldStack', { });

ステップ 2: Lambda 関数の作成

CDK プロジェクト内で、新しい hello.js ファイルを含む lambda ディレクトリを作成します。以下に例を示します。

TypeScript

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js
JavaScript

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js
Python

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js
Java

プロジェクトのルートから次のコマンドを実行します。

$ mkdir -p src/main/resources/lambda $ cd src/main/resources/lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── src └── main └──resources └──lambda └──hello.js
C#

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js
Go

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js

プロジェクトのルートから次のコマンドを実行します。

$ mkdir lambda && cd lambda $ touch hello.js

次のものは CDK プロジェクトに追加されます。

cdk-hello-world └── lambda └── hello.js
注記

このチュートリアルを簡単にするため、すべての CDK プログラミング言語に JavaScript Lambda 関数を使用します。

新しく作成されたファイルに次の内容を追加することにより、Lambda 関数を定義します。

exports.handler = async (event) => { return { statusCode: 200, headers: { "Content-Type": "text/plain" }, body: JSON.stringify({ message: "Hello, World!" }), }; };

ステップ 3: コンストラクトの定義

このステップでは、 AWS CDK L2 コンストラクトを使用して Lambda および API Gateway リソースを定義します。

CDK スタックを定義するプロジェクトファイルを開きます。このファイルを変更してコンストラクトを定義します。次の内容は、開始スタックファイルの例です。

TypeScript

lib/cdk-hello-world-stack.ts にあります

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Your constructs will go here } }
JavaScript

lib/cdk-hello-world-stack.js にあります

const { Stack, Duration } = require('aws-cdk-lib'); const lambda = require('aws-cdk-lib/aws-lambda'); const apigateway = require('aws-cdk-lib/aws-apigateway'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Your constructs will go here } } module.exports = { CdkHelloWorldStack }
Python

cdk_hello_world/cdk_hello_world_stack.py にあります

from aws_cdk import Stack from constructs import Construct class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) // Your constructs will go here
Java

src/main/java/.../CdkHelloWorldStack.java にあります

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Your constructs will go here } }
C#

src/CdkHelloWorld/CdkHelloWorldStack.cs にあります

using Amazon.CDK; using Constructs; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Your constructs will go here } } }
Go

cdk-hello-world.go にあります

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type CdkHelloWorldStackProps struct { awscdk.StackProps } func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Your constructs will go here return stack } func main() { // ... } func env() *awscdk.Environment { return nil }

lib/cdk-hello-world-stack.ts にあります

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Your constructs will go here } }

このファイルでは、 AWS CDK は以下を実行しています。

  • CDK スタックインスタンスは Stack クラスからインスタンス化されます。

  • Constructs ベースクラスはインポートされ、スタックインスタンスのスコープまたは親として提供されます。

Lambda 関数リソースの定義

Lambda 関数リソースを定義するには、 AWS コンストラクトライブラリから aws-lambda L2 コンストラクトをインポートして使用します。

スタックファイルを次のように変更します。

TypeScript
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; // Import Lambda L2 construct import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the Lambda function resource const helloWorldFunction = new lambda.Function(this, 'HelloWorldFunction', { runtime: lambda.Runtime.NODEJS_20_X, // Choose any supported Node.js runtime code: lambda.Code.fromAsset('lambda'), // Points to the lambda directory handler: 'hello.handler', // Points to the 'hello' file in the lambda directory }); } }
JavaScript
const { Stack, Duration } = require('aws-cdk-lib'); // Import Lambda L2 construct const lambda = require('aws-cdk-lib/aws-lambda'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define the Lambda function resource const helloWorldFunction = new lambda.Function(this, 'HelloWorldFunction', { runtime: lambda.Runtime.NODEJS_20_X, // Choose any supported Node.js runtime code: lambda.Code.fromAsset('lambda'), // Points to the lambda directory handler: 'hello.handler', // Points to the 'hello' file in the lambda directory }); } } module.exports = { CdkHelloWorldStack }
Python
from aws_cdk import ( Stack, # Import Lambda L2 construct aws_lambda as _lambda, ) # ... class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define the Lambda function resource hello_world_function = _lambda.Function( self, "HelloWorldFunction", runtime = _lambda.Runtime.NODEJS_20_X, # Choose any supported Node.js runtime code = _lambda.Code.from_asset("lambda"), # Points to the lambda directory handler = "hello.handler", # Points to the 'hello' file in the lambda directory )
注記

lambda は Python のビルトイン識別子であるため、_lambda として aws_lambda モジュールをインポートします。

Java
// ... // Import Lambda L2 construct import software.amazon.awscdk.services.lambda.Code; import software.amazon.awscdk.services.lambda.Function; import software.amazon.awscdk.services.lambda.Runtime; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define the Lambda function resource Function helloWorldFunction = Function.Builder.create(this, "HelloWorldFunction") .runtime(Runtime.NODEJS_20_X) // Choose any supported Node.js runtime .code(Code.fromAsset("src/main/resources/lambda")) // Points to the lambda directory .handler("hello.handler") // Points to the 'hello' file in the lambda directory .build(); } }
C#
// ... // Import Lambda L2 construct using Amazon.CDK.AWS.Lambda; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define the Lambda function resource var helloWorldFunction = new Function(this, "HelloWorldFunction", new FunctionProps { Runtime = Runtime.NODEJS_20_X, // Choose any supported Node.js runtime Code = Code.FromAsset("lambda"), // Points to the lambda directory Handler = "hello.handler" // Points to the 'hello' file in the lambda directory }); } } }
Go
package main import ( // ... // Import Lambda L2 construct "github.com/aws/aws-cdk-go/awscdk/v2/awslambda" // Import S3 assets construct "github.com/aws/aws-cdk-go/awscdk/v2/awss3assets" // ... ) // ... func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define the Lambda function resource helloWorldFunction := awslambda.NewFunction(stack, jsii.String("HelloWorldFunction"), &awslambda.FunctionProps{ Runtime: awslambda.Runtime_NODEJS_20_X(), // Choose any supported Node.js runtime Code: awslambda.Code_FromAsset(jsii.String("lambda"), &awss3assets.AssetOptions{}), // Points to the lambda directory Handler: jsii.String("hello.handler"), // Points to the 'hello' file in the lambda directory }) return stack } // ...
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; // Import Lambda L2 construct import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the Lambda function resource const helloWorldFunction = new lambda.Function(this, 'HelloWorldFunction', { runtime: lambda.Runtime.NODEJS_20_X, // Choose any supported Node.js runtime code: lambda.Code.fromAsset('lambda'), // Points to the lambda directory handler: 'hello.handler', // Points to the 'hello' file in the lambda directory }); } }

こちらでは、Lambda 関数リソースを作成して次のプロパティを定義します。

  • runtime – 関数が実行される環境。こちらでは、Node.js のバージョン 20.x を使用します。

  • code – ローカルマシンの関数コードへのパス。

  • handler – 関数コードを含む特定のファイルの名前。

API Gateway REST API リソースの定義

API Gateway REST API リソースを定義するには、 AWS コンストラクトライブラリから aws-apigateway L2 コンストラクトをインポートして使用します。

スタックファイルを次のように変更します。

TypeScript
// ... //Import API Gateway L2 construct import * as apigateway from 'aws-cdk-lib/aws-apigateway'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // ... // Define the API Gateway resource const api = new apigateway.LambdaRestApi(this, 'HelloWorldApi', { handler: helloWorldFunction, proxy: false, }); // Define the '/hello' resource with a GET method const helloResource = api.root.addResource('hello'); helloResource.addMethod('GET'); } }
JavaScript
// ... // Import API Gateway L2 construct const apigateway = require('aws-cdk-lib/aws-apigateway'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // ... // Define the API Gateway resource const api = new apigateway.LambdaRestApi(this, 'HelloWorldApi', { handler: helloWorldFunction, proxy: false, }); // Define the '/hello' resource with a GET method const helloResource = api.root.addResource('hello'); helloResource.addMethod('GET'); }; }; // ...
Python
from aws_cdk import ( # ... # Import API Gateway L2 construct aws_apigateway as apigateway, ) from constructs import Construct class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # ... # Define the API Gateway resource api = apigateway.LambdaRestApi( self, "HelloWorldApi", handler = hello_world_function, proxy = False, ) # Define the '/hello' resource with a GET method hello_resource = api.root.add_resource("hello") hello_resource.add_method("GET")
Java
// ... // Import API Gateway L2 construct import software.amazon.awscdk.services.apigateway.LambdaRestApi; import software.amazon.awscdk.services.apigateway.Resource; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // ... // Define the API Gateway resource LambdaRestApi api = LambdaRestApi.Builder.create(this, "HelloWorldApi") .handler(helloWorldFunction) .proxy(false) // Turn off default proxy integration .build(); // Define the '/hello' resource and its GET method Resource helloResource = api.getRoot().addResource("hello"); helloResource.addMethod("GET"); } }
C#
// ... // Import API Gateway L2 construct using Amazon.CDK.AWS.APIGateway; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // ... // Define the API Gateway resource var api = new LambdaRestApi(this, "HelloWorldApi", new LambdaRestApiProps { Handler = helloWorldFunction, Proxy = false }); // Add a '/hello' resource with a GET method var helloResource = api.Root.AddResource("hello"); helloResource.AddMethod("GET"); } } }
Go

// ... import ( // ... // Import Api Gateway L2 construct "github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway" // ... ) // ... func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define the Lambda function resource // ... // Define the API Gateway resource api := awsapigateway.NewLambdaRestApi(stack, jsii.String("HelloWorldApi"), &awsapigateway.LambdaRestApiProps{ Handler: helloWorldFunction, Proxy: jsii.Bool(false), }) // Add a '/hello' resource with a GET method helloResource := api.Root().AddResource(jsii.String("hello"), &awsapigateway.ResourceOptions{}) helloResource.AddMethod(jsii.String("GET"), awsapigateway.NewLambdaIntegration(helloWorldFunction, &awsapigateway.LambdaIntegrationOptions{}), &awsapigateway.MethodOptions{}) return stack } // ...
// ... //Import API Gateway L2 construct import * as apigateway from 'aws-cdk-lib/aws-apigateway'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // ... // Define the API Gateway resource const api = new apigateway.LambdaRestApi(this, 'HelloWorldApi', { handler: helloWorldFunction, proxy: false, }); // Define the '/hello' resource with a GET method const helloResource = api.root.addResource('hello'); helloResource.addMethod('GET'); } }

こちらでは、API Gateway REST API リソースに加えて次のものを作成します。

  • REST API と Lambda 関数間の統合は、API が関数を呼び出すことを可能にします。これには、Lambda アクセス許可リソースの作成が含まれます。

  • API エンドポイントのルートに追加される hello という名前の新しいリソースまたはパス。ベースの URL に /hello を追加する新しいエンドポイントが作成されます。

  • hello リソースのGET メソッド。GET 要求が /hello エンドポイントに送信されると、Lambda 関数が呼び出されてレスポンスが返されます。

ステップ 4: アプリケーションをデプロイのための準備

このステップでは、必要に応じてアプリケーションをビルドし、 cdk synth コマンドを使用して AWS CDK CLI基本的な検証を実行して、デプロイ用にアプリケーションを準備します。

必要に応じて、アプリケーションを構築します。

TypeScript

プロジェクトのルートから次のコマンドを実行します。

$ npm run build
JavaScript

構築は必要ありません。

Python

構築は必要ありません。

Java

プロジェクトのルートから次のコマンドを実行します。

$ mvn package
C#

プロジェクトのルートから次のコマンドを実行します。

$ dotnet build src
Go

構築は必要ありません。

プロジェクトのルートから次のコマンドを実行します。

$ npm run build

cdk synth を実行して、CDK コードから AWS CloudFormation テンプレートを合成します。L2 コンストラクトを使用すると、Lambda 関数と 間のやり取りを容易に AWS CloudFormation するために が必要とする設定の詳細の多くはREST API、 によってプロビジョニングされます AWS CDK。

プロジェクトのルートから次のコマンドを実行します。

$ cdk synth
注記

次のようなエラーが表示された場合、cdk-hello-world ディレクトリにいることを確認してもう一度やり直してください。

--app is required either in command-line, in cdk.json or in ~/.cdk.json

成功すると、 AWS CDK CLIはコマンドプロンプトで AWS CloudFormation テンプレートを YAML 形式で出力します。JSON にフォーマットされたテンプレートも cdk.out ディレクトリに保存されます。

テンプレートの出力例を次に示します AWS CloudFormation 。

Resources: HelloWorldFunctionServiceRoleunique-identifier: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: lambda.amazonaws.com Version: "2012-10-17" ManagedPolicyArns: - Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/ServiceRole/Resource HelloWorldFunctionunique-identifier: Type: AWS::Lambda::Function Properties: Code: S3Bucket: Fn::Sub: cdk-unique-identifier-assets-${AWS::AccountId}-${AWS::Region} S3Key: unique-identifier.zip Handler: hello.handler Role: Fn::GetAtt: - HelloWorldFunctionServiceRoleunique-identifier - Arn Runtime: nodejs20.x DependsOn: - HelloWorldFunctionServiceRoleunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/Resource aws:asset:path: asset.unique-identifier aws:asset:is-bundled: false aws:asset:property: Code HelloWorldApiunique-identifier: Type: AWS::ApiGateway::RestApi Properties: Name: HelloWorldApi Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Resource HelloWorldApiDeploymentunique-identifier: Type: AWS::ApiGateway::Deployment Properties: Description: Automatically created by the RestApi construct RestApiId: Ref: HelloWorldApiunique-identifier DependsOn: - HelloWorldApihelloGETunique-identifier - HelloWorldApihellounique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Deployment/Resource HelloWorldApiDeploymentStageprod012345ABC: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: HelloWorldApiDeploymentunique-identifier RestApiId: Ref: HelloWorldApiunique-identifier StageName: prod Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/DeploymentStage.prod/Resource HelloWorldApihellounique-identifier: Type: AWS::ApiGateway::Resource Properties: ParentId: Fn::GetAtt: - HelloWorldApiunique-identifier - RootResourceId PathPart: hello RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/Resource HelloWorldApihelloGETApiPermissionCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApi9E278160 - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - /GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETApiPermissionTestCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApiunique-identifier - /test-invoke-stage/GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.Test.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETunique-identifier: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: GET Integration: IntegrationHttpMethod: POST Type: AWS_PROXY Uri: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":apigateway:" - Ref: AWS::Region - :lambda:path/2015-03-31/functions/ - Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn - /invocations ResourceId: Ref: HelloWorldApihellounique-identifier RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/Resource CDKMetadata: Type: AWS::CDK::Metadata Properties: Analytics: v2:deflate64:unique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/CDKMetadata/Default Condition: CDKMetadataAvailable Outputs: HelloWorldApiEndpointunique-identifier: Value: Fn::Join: - "" - - https:// - Ref: HelloWorldApiunique-identifier - .execute-api. - Ref: AWS::Region - "." - Ref: AWS::URLSuffix - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - / Conditions: CDKMetadataAvailable: Fn::Or: - Fn::Or: - Fn::Equals: - Ref: AWS::Region - af-south-1 - Fn::Equals: - Ref: AWS::Region - ap-east-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-2 - Fn::Equals: - Ref: AWS::Region - ap-south-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-2 - Fn::Equals: - Ref: AWS::Region - ca-central-1 - Fn::Equals: - Ref: AWS::Region - cn-north-1 - Fn::Equals: - Ref: AWS::Region - cn-northwest-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - eu-central-1 - Fn::Equals: - Ref: AWS::Region - eu-north-1 - Fn::Equals: - Ref: AWS::Region - eu-south-1 - Fn::Equals: - Ref: AWS::Region - eu-west-1 - Fn::Equals: - Ref: AWS::Region - eu-west-2 - Fn::Equals: - Ref: AWS::Region - eu-west-3 - Fn::Equals: - Ref: AWS::Region - il-central-1 - Fn::Equals: - Ref: AWS::Region - me-central-1 - Fn::Equals: - Ref: AWS::Region - me-south-1 - Fn::Equals: - Ref: AWS::Region - sa-east-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - us-east-1 - Fn::Equals: - Ref: AWS::Region - us-east-2 - Fn::Equals: - Ref: AWS::Region - us-west-1 - Fn::Equals: - Ref: AWS::Region - us-west-2 Parameters: BootstrapVersion: Type: AWS::SSM::Parameter::Value<String> Default: /cdk-bootstrap/hnb659fds/version Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] Rules: CheckBootstrapVersion: Assertions: - Assert: Fn::Not: - Fn::Contains: - - "1" - "2" - "3" - "4" - "5" - Ref: BootstrapVersion AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.

Resources: HelloWorldFunctionServiceRoleunique-identifier: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: lambda.amazonaws.com Version: "2012-10-17" ManagedPolicyArns: - Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/ServiceRole/Resource HelloWorldFunctionunique-identifier: Type: AWS::Lambda::Function Properties: Code: S3Bucket: Fn::Sub: cdk-unique-identifier-assets-${AWS::AccountId}-${AWS::Region} S3Key: unique-identifier.zip Handler: hello.handler Role: Fn::GetAtt: - HelloWorldFunctionServiceRoleunique-identifier - Arn Runtime: nodejs20.x DependsOn: - HelloWorldFunctionServiceRoleunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/Resource aws:asset:path: asset.unique-identifier aws:asset:is-bundled: false aws:asset:property: Code HelloWorldApiunique-identifier: Type: AWS::ApiGateway::RestApi Properties: Name: HelloWorldApi Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Resource HelloWorldApiDeploymentunique-identifier: Type: AWS::ApiGateway::Deployment Properties: Description: Automatically created by the RestApi construct RestApiId: Ref: HelloWorldApiunique-identifier DependsOn: - HelloWorldApihelloGETunique-identifier - HelloWorldApihellounique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Deployment/Resource HelloWorldApiDeploymentStageprod012345ABC: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: HelloWorldApiDeploymentunique-identifier RestApiId: Ref: HelloWorldApiunique-identifier StageName: prod Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/DeploymentStage.prod/Resource HelloWorldApihellounique-identifier: Type: AWS::ApiGateway::Resource Properties: ParentId: Fn::GetAtt: - HelloWorldApiunique-identifier - RootResourceId PathPart: hello RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/Resource HelloWorldApihelloGETApiPermissionCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApi9E278160 - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - /GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETApiPermissionTestCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApiunique-identifier - /test-invoke-stage/GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.Test.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETunique-identifier: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: GET Integration: IntegrationHttpMethod: POST Type: AWS_PROXY Uri: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":apigateway:" - Ref: AWS::Region - :lambda:path/2015-03-31/functions/ - Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn - /invocations ResourceId: Ref: HelloWorldApihellounique-identifier RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/Resource CDKMetadata: Type: AWS::CDK::Metadata Properties: Analytics: v2:deflate64:unique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/CDKMetadata/Default Condition: CDKMetadataAvailable Outputs: HelloWorldApiEndpointunique-identifier: Value: Fn::Join: - "" - - https:// - Ref: HelloWorldApiunique-identifier - .execute-api. - Ref: AWS::Region - "." - Ref: AWS::URLSuffix - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - / Conditions: CDKMetadataAvailable: Fn::Or: - Fn::Or: - Fn::Equals: - Ref: AWS::Region - af-south-1 - Fn::Equals: - Ref: AWS::Region - ap-east-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-2 - Fn::Equals: - Ref: AWS::Region - ap-south-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-2 - Fn::Equals: - Ref: AWS::Region - ca-central-1 - Fn::Equals: - Ref: AWS::Region - cn-north-1 - Fn::Equals: - Ref: AWS::Region - cn-northwest-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - eu-central-1 - Fn::Equals: - Ref: AWS::Region - eu-north-1 - Fn::Equals: - Ref: AWS::Region - eu-south-1 - Fn::Equals: - Ref: AWS::Region - eu-west-1 - Fn::Equals: - Ref: AWS::Region - eu-west-2 - Fn::Equals: - Ref: AWS::Region - eu-west-3 - Fn::Equals: - Ref: AWS::Region - il-central-1 - Fn::Equals: - Ref: AWS::Region - me-central-1 - Fn::Equals: - Ref: AWS::Region - me-south-1 - Fn::Equals: - Ref: AWS::Region - sa-east-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - us-east-1 - Fn::Equals: - Ref: AWS::Region - us-east-2 - Fn::Equals: - Ref: AWS::Region - us-west-1 - Fn::Equals: - Ref: AWS::Region - us-west-2 Parameters: BootstrapVersion: Type: AWS::SSM::Parameter::Value<String> Default: /cdk-bootstrap/hnb659fds/version Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] Rules: CheckBootstrapVersion: Assertions: - Assert: Fn::Not: - Fn::Contains: - - "1" - "2" - "3" - "4" - "5" - Ref: BootstrapVersion AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.

L2 コンストラクトを使用すると、リソースを設定するためのいくつかのプロパティを定義し、ヘルパーメソッドを使用して統合できます。は、アプリケーションのプロビジョニングに必要な AWS CloudFormation リソースとプロパティの大部分 AWS CDK を設定します。

ステップ 5: アプリケーションをデプロイする

このステップでは、 cdk deploy コマンドを使用して AWS CDK CLIアプリケーションをデプロイします。 AWS CDK は AWS CloudFormation サービスと連携してリソースをプロビジョニングします。

重要

デプロイする前に、 AWS 環境の 1 回限りのブートストラップを実行する必要があります。手順については、で使用する環境をブートストラップする AWS CDK を参照してください。

プロジェクトのルートから次のコマンドを実行します。プロンプトが表示されたら変更を確認します。

$ cdk deploy ✨ Synthesis time: 2.44s ... Do you wish to deploy these changes (y/n)? y

デプロイが完了すると、 AWS CDK CLIはエンドポイント URL を出力します。次のステップ用にこの URL をコピーします。以下に例を示します。

... ✅ HelloWorldStack ✨ Deployment time: 45.37s Outputs: HelloWorldStack.HelloWorldApiEndpointunique-identifier = https://<api-id>.execute-api.<region>.amazonaws.com/prod/ Stack ARN: arn:aws:cloudformation:region:account-id:stack/HelloWorldStack/unique-identifier ...

ステップ 6: アプリケーションの操作

このステップでは、API エンドポイントに GET 要求を送信し、Lambda 関数のレスポンスを受信します。

前のステップからエンドポイント URL を見つけ、/hello パスを追加します。次に、ブラウザまたはコマンドプロンプトを使用して、エンドポイントに GET 要求を送信します。以下に例を示します。

$ curl https://<api-id>.execute-api.<region>.amazonaws.com/prod/hello {"message":"Hello World!"}%

おめでとうございます。 AWS CDKを使用してアプリケーションの作成、デプロイ、操作が正常に完了しました。

ステップ 7: アプリケーションの削除

このステップでは、 を使用して AWS CDK CLIからアプリケーションを削除します AWS クラウド。

アプリケーションを削除するには、cdk destroy を実行します。プロンプトが表示されたら、アプリケーションを削除する要求を確認します。

$ cdk destroy Are you sure you want to delete: CdkHelloWorldStack (y/n)? y CdkHelloWorldStack: destroying... [1/1] ... ✅ CdkHelloWorldStack: destroyed

トラブルシューティング

Error: {"message": "Internal server error"}%

デプロイされた Lambda 関数を呼び出すとき、このエラーが表示されます。このエラーは、いくつかの理由で発生する可能性があります。

さらにトラブルシューティングを行う方法

を使用して Lambda 関数を AWS CLI 呼び出します。

  1. スタックファイルを変更し、デプロイされた Lambda 関数名の出力値をキャプチャします。以下に例を示します。

    ... class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define the Lambda function resource // ... new CfnOutput(this, 'HelloWorldFunctionName', { value: helloWorldFunction.functionName, description: 'JavaScript Lambda function' }); // Define the API Gateway resource // ...
  2. アプリケーションを再度デプロイします。 AWS CDK CLI は、デプロイされた Lambda 関数名の値を出力します。

    $ cdk deploy ✨ Synthesis time: 0.29s ... ✅ CdkHelloWorldStack ✨ Deployment time: 20.36s Outputs: ... CdkHelloWorldStack.HelloWorldFunctionName = CdkHelloWorldStack-HelloWorldFunctionunique-identifier ...
  3. を使用して AWS CLI で Lambda 関数を呼び出し AWS クラウド 、レスポンスをテキストファイルに出力します。

    $ aws lambda invoke --function-name CdkHelloWorldStack-HelloWorldFunctionunique-identifier output.txt
  4. output.txt をチェックして結果を確認します。

考えられる原因: API Gateway リソースがスタックファイルで正しく定義されていません。

output.txt が成功した Lambda 関数のレスポンスを表示した場合、API Gateway REST API の定義方法に問題がある可能性があります。は、エンドポイントではなく Lambda を直接 AWS CLI 呼び出します。コードがこのチュートリアルと一致していることを確認してください。次に、再度デプロイします。

考えられる原因: Lambda リソースがスタックファイルで正しく定義されていません。

output.txt がエラーを返した場合、Lambda 関数の定義方法に問題がある可能性があります。コードがこのチュートリアルと一致していることを確認してください。次に、再度デプロイします。

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.