{"id":3891,"date":"2026-11-20T05:29:51","date_gmt":"2026-11-19T20:29:51","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=3891"},"modified":"2026-11-20T05:29:51","modified_gmt":"2026-11-19T20:29:51","slug":"20261119-plan123","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2026\/11\/20\/20261119-plan123\/","title":{"rendered":"API Gateway + Lambda \u3067 REST API \u3092\u69cb\u7bc9\u3059\u308b\u3010AWS SAM \u30cf\u30f3\u30ba\u30aa\u30f3\u3011"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>Amazon API Gateway\uff08REST API\uff09\u3068 AWS Lambda\u3001DynamoDB \u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u30a2\u30a4\u30c6\u30e0\u3092 CRUD \u3059\u308b\u5c0f\u3055\u306a REST API \u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30a4\u30f3\u30d5\u30e9\u306f AWS SAM \u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c81\u679a\u3067\u5b9a\u7fa9\u3057\u307e\u3059<\/li>\n<li>Lambda \u306f Python 3.12 \u3067\u66f8\u304d\u3001Lambda \u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u3067 API Gateway \u3068\u3064\u306a\u304e\u307e\u3059<\/li>\n<li>\u30c7\u30d7\u30ed\u30a4\u5f8c\u306b curl \u3067\u52d5\u4f5c\u3092\u78ba\u8a8d\u3057\u3001\u6700\u5f8c\u306b\u30b9\u30bf\u30c3\u30af\u3054\u3068\u524a\u9664\u3057\u307e\u3059<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th>\u30e1\u30bd\u30c3\u30c9<\/th>\n<th>\u30d1\u30b9<\/th>\n<th>\u5185\u5bb9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>GET<\/td>\n<td><code>\/items<\/code><\/td>\n<td>\u4e00\u89a7\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td>POST<\/td>\n<td><code>\/items<\/code><\/td>\n<td>\u65b0\u898f\u4f5c\u6210<\/td>\n<\/tr>\n<tr>\n<td>GET<\/td>\n<td><code>\/items\/{id}<\/code><\/td>\n<td>1\u4ef6\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td>PUT<\/td>\n<td><code>\/items\/{id}<\/code><\/td>\n<td>\u66f4\u65b0<\/td>\n<\/tr>\n<tr>\n<td>DELETE<\/td>\n<td><code>\/items\/{id}<\/code><\/td>\n<td>\u524a\u9664<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&gt; <strong>\u6ce8\u610f<\/strong>\uff1a\u3053\u306e\u30cf\u30f3\u30ba\u30aa\u30f3\u306e API \u306f\u8a8d\u8a3c\u306a\u3057\u3067\u516c\u958b\u3055\u308c\u307e\u3059\u3002\u691c\u8a3c\u304c\u7d42\u308f\u3063\u305f\u3089\u5fc5\u305a <code>sam delete<\/code> \u3067\u524a\u9664\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u74b0\u5883<\/h2>\n<ul>\n<li>AWS \u30a2\u30ab\u30a6\u30f3\u30c8\uff08IAM \u306e\u4f5c\u6210\u6a29\u9650\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u304b\u30ed\u30fc\u30eb\uff09<\/li>\n<li>AWS CLI v2\uff08\u8a8d\u8a3c\u8a2d\u5b9a\u6e08\u307f\uff09<\/li>\n<li>AWS SAM CLI<\/li>\n<li>Python 3.12<\/li>\n<li>curl \/ jq<\/li>\n<\/ul>\n<p>\u30ea\u30fc\u30b8\u30e7\u30f3\u306f <code>ap-northeast-1<\/code> \u3092\u4f8b\u306b\u3057\u307e\u3059\u3002<\/p>\n<h2>\u69cb\u6210<\/h2>\n<pre class=\"lang:default decode:true\">\u30af\u30e9\u30a4\u30a2\u30f3\u30c8 (curl)\n   \u2502  HTTPS\n   \u25bc\nAPI Gateway (REST API \/ \u30b9\u30c6\u30fc\u30b8: dev)\n   \u2502  Lambda \u30d7\u30ed\u30ad\u30b7\u7d71\u5408\n   \u25bc\nLambda (Python 3.12, arm64)\n   \u2502  boto3\n   \u25bc\nDynamoDB (\u30aa\u30f3\u30c7\u30de\u30f3\u30c9)<\/pre>\n<h3>REST API \u3068 HTTP API \u306e\u9055\u3044<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u89b3\u70b9<\/th>\n<th>REST API<\/th>\n<th>HTTP API<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6599\u91d1\u30fb\u30ec\u30a4\u30c6\u30f3\u30b7<\/td>\n<td>\u6bd4\u8f03\u7684\u9ad8\u3044<\/td>\n<td>\u5b89\u304f\u3001\u8efd\u91cf<\/td>\n<\/tr>\n<tr>\n<td>API \u30ad\u30fc\u30fb\u4f7f\u7528\u91cf\u30d7\u30e9\u30f3<\/td>\n<td>\u3042\u308a<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>\u30ea\u30af\u30a8\u30b9\u30c8\u691c\u8a3c<\/td>\n<td>\u3042\u308a<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>AWS WAF \u9023\u643a<\/td>\n<td>\u3042\u308a<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>\u30ec\u30b9\u30dd\u30f3\u30b9\u30ad\u30e3\u30c3\u30b7\u30e5<\/td>\n<td>\u3042\u308a<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<tr>\n<td>\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8<\/td>\n<td>\u3042\u308a<\/td>\n<td>\u306a\u3057<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Lambda \u3092\u547c\u3076\u3060\u3051\u3067 JWT \u8a8d\u8a3c\u304c\u3042\u308c\u3070\u5341\u5206\u306a\u3089\u3001HTTP API \u3067\u8db3\u308a\u307e\u3059\u3002\u4f7f\u7528\u91cf\u30d7\u30e9\u30f3\u3084 WAF \u304c\u5fc5\u8981\u306a\u5834\u5408\u306f REST API \u3092\u9078\u3073\u307e\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f REST API \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h2>\u624b\u9806<\/h2>\n<h3>1. \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u308b<\/h3>\n<pre class=\"lang:default decode:true\">mkdir items-api &amp;&amp; cd items-api\nmkdir src<\/pre>\n<pre class=\"lang:default decode:true\">items-api\/\n\u251c\u2500\u2500 template.yaml\n\u2514\u2500\u2500 src\/\n    \u2514\u2500\u2500 app.py<\/pre>\n<h3>2. SAM \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u66f8\u304f<\/h3>\n<p><code>template.yaml<\/code><\/p>\n<pre class=\"lang:default decode:true\">AWSTemplateFormatVersion: \"2010-09-09\"\nTransform: AWS::Serverless-2016-10-31\nDescription: Items REST API (API Gateway + Lambda + DynamoDB)\n\nGlobals:\n  Function:\n    Runtime: python3.12\n    Architectures: [arm64]\n    Timeout: 10\n    MemorySize: 256\n\nResources:\n  ItemsApi:\n    Type: AWS::Serverless::Api\n    Properties:\n      StageName: dev\n      MethodSettings:\n        - ResourcePath: \"\/*\"\n          HttpMethod: \"*\"\n          ThrottlingRateLimit: 10\n          ThrottlingBurstLimit: 20\n\n  ItemsFunction:\n    Type: AWS::Serverless::Function\n    Properties:\n      CodeUri: src\/\n      Handler: app.handler\n      Environment:\n        Variables:\n          TABLE_NAME: !Ref ItemsTable\n      Policies:\n        - DynamoDBCrudPolicy:\n            TableName: !Ref ItemsTable\n      Events:\n        ListItems:\n          Type: Api\n          Properties:\n            RestApiId: !Ref ItemsApi\n            Path: \/items\n            Method: get\n        CreateItem:\n          Type: Api\n          Properties:\n            RestApiId: !Ref ItemsApi\n            Path: \/items\n            Method: post\n        GetItem:\n          Type: Api\n          Properties:\n            RestApiId: !Ref ItemsApi\n            Path: \/items\/{id}\n            Method: get\n        UpdateItem:\n          Type: Api\n          Properties:\n            RestApiId: !Ref ItemsApi\n            Path: \/items\/{id}\n            Method: put\n        DeleteItem:\n          Type: Api\n          Properties:\n            RestApiId: !Ref ItemsApi\n            Path: \/items\/{id}\n            Method: delete\n\n  ItemsTable:\n    Type: AWS::DynamoDB::Table\n    Properties:\n      BillingMode: PAY_PER_REQUEST\n      AttributeDefinitions:\n        - AttributeName: id\n          AttributeType: S\n      KeySchema:\n        - AttributeName: id\n          KeyType: HASH\n\nOutputs:\n  ApiUrl:\n    Description: API endpoint\n    Value: !Sub \"https:\/\/${ItemsApi}.execute-api.${AWS::Region}.amazonaws.com\/dev\"<\/pre>\n<p>\u30dd\u30a4\u30f3\u30c8:<\/p>\n<ul>\n<li><code>Events<\/code> \u306e <code>Type: Api<\/code> \u3092\u66f8\u304f\u3068\u3001SAM \u304c\u30ea\u30bd\u30fc\u30b9\u30fb\u30e1\u30bd\u30c3\u30c9\u30fbLambda \u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u306b\u52a0\u3048\u3066\u3001<strong>API Gateway \u304b\u3089 Lambda \u3092\u547c\u3076\u6a29\u9650<\/strong>\u307e\u3067\u4f5c\u308a\u307e\u3059\u3002\u624b\u4f5c\u696d\u3067\u306f\u6700\u3082\u629c\u3051\u3084\u3059\u3044\u90e8\u5206\u3067\u3059\u3002<\/li>\n<li><code>DynamoDBCrudPolicy<\/code> \u3067\u3001\u6a29\u9650\u3092\u5bfe\u8c61\u30c6\u30fc\u30d6\u30eb\u3060\u3051\u306b\u7d5e\u308c\u307e\u3059\u3002<\/li>\n<li><code>MethodSettings<\/code> \u306e\u30b9\u30ed\u30c3\u30c8\u30ea\u30f3\u30b0\u306f\u3001\u610f\u56f3\u3057\u306a\u3044\u5927\u91cf\u30a2\u30af\u30bb\u30b9\u3078\u306e\u4fdd\u967a\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>3. Lambda \u95a2\u6570\u3092\u66f8\u304f<\/h3>\n<p><code>src\/app.py<\/code><\/p>\n<pre class=\"lang:default decode:true\">import json\nimport logging\nimport os\nimport uuid\nfrom decimal import Decimal\n\nimport boto3\nfrom botocore.exceptions import ClientError\n\nlogger = logging.getLogger()\nlogger.setLevel(logging.INFO)\n\n# \u30b3\u30fc\u30eb\u30c9\u30b9\u30bf\u30fc\u30c8\u6642\u306b1\u56de\u3060\u3051\u521d\u671f\u5316\u3057\u3001\u30a6\u30a9\u30fc\u30e0\u306a\u5b9f\u884c\u3067\u306f\u518d\u5229\u7528\u3059\u308b\ntable = boto3.resource(\"dynamodb\").Table(os.environ[\"TABLE_NAME\"])\n\n\ndef _json_default(o):\n    # DynamoDB \u306e\u6570\u5024\u306f Decimal \u3067\u8fd4\u308b\u305f\u3081\u3001JSON \u5316\u3067\u304d\u308b\u578b\u306b\u5909\u63db\u3059\u308b\n    if isinstance(o, Decimal):\n        return int(o) if o == o.to_integral_value() else float(o)\n    raise TypeError(f\"Object of type {type(o).__name__} is not JSON serializable\")\n\n\ndef response(status, body=None):\n    # \u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u5f62\u5f0f: statusCode \/ headers \/ body(\u6587\u5b57\u5217)\n    return {\n        \"statusCode\": status,\n        \"headers\": {\"Content-Type\": \"application\/json\"},\n        \"body\": \"\" if body is None else json.dumps(body, ensure_ascii=False, default=_json_default),\n    }\n\n\ndef parse_body(event):\n    raw = event.get(\"body\") or \"{}\"\n    # float \u306e\u307e\u307e\u3060\u3068 DynamoDB \u306b\u66f8\u304d\u8fbc\u3081\u306a\u3044\u306e\u3067 Decimal \u3067\u53d7\u3051\u308b\n    return json.loads(raw, parse_float=Decimal)\n\n\ndef validate(body):\n    name = body.get(\"name\")\n    if not isinstance(name, str) or not name.strip():\n        return \"name is required\"\n    price = body.get(\"price\", 0)\n    if isinstance(price, bool) or not isinstance(price, (int, Decimal)) or price &lt; 0:\n        return \"price must be a non-negative number\"\n    return None\n\n\ndef list_items():\n    # Scan \u306f\u5168\u4ef6\u8aad\u3080\u306e\u3067\u3001\u672c\u756a\u3067\u306f\u30af\u30a8\u30ea\u8a2d\u8a08\u3092\u898b\u76f4\u3059\u3053\u3068\n    res = table.scan(Limit=100)\n    return response(200, {\"items\": res.get(\"Items\", [])})\n\n\ndef create_item(body):\n    error = validate(body)\n    if error:\n        return response(400, {\"message\": error})\n    item = {\"id\": str(uuid.uuid4()), \"name\": body[\"name\"], \"price\": body.get(\"price\", 0)}\n    table.put_item(Item=item)\n    return response(201, item)\n\n\ndef get_item(item_id):\n    item = table.get_item(Key={\"id\": item_id}).get(\"Item\")\n    if item is None:\n        return response(404, {\"message\": \"not found\"})\n    return response(200, item)\n\n\ndef update_item(item_id, body):\n    error = validate(body)\n    if error:\n        return response(400, {\"message\": error})\n    try:\n        res = table.update_item(\n            Key={\"id\": item_id},\n            # name \u306f DynamoDB \u306e\u4e88\u7d04\u8a9e\u306a\u306e\u3067\u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u3067\u66f8\u304f\n            UpdateExpression=\"SET #n = :name, price = :price\",\n            ExpressionAttributeNames={\"#n\": \"name\"},\n            ExpressionAttributeValues={\":name\": body[\"name\"], \":price\": body.get(\"price\", 0)},\n            ConditionExpression=\"attribute_exists(id)\",\n            ReturnValues=\"ALL_NEW\",\n        )\n    except ClientError as e:\n        if e.response[\"Error\"][\"Code\"] == \"ConditionalCheckFailedException\":\n            return response(404, {\"message\": \"not found\"})\n        raise\n    return response(200, res[\"Attributes\"])\n\n\ndef delete_item(item_id):\n    try:\n        table.delete_item(Key={\"id\": item_id}, ConditionExpression=\"attribute_exists(id)\")\n    except ClientError as e:\n        if e.response[\"Error\"][\"Code\"] == \"ConditionalCheckFailedException\":\n            return response(404, {\"message\": \"not found\"})\n        raise\n    return response(204)\n\n\ndef handler(event, context):\n    method = event[\"httpMethod\"]\n    resource = event[\"resource\"]  # \u4f8b: \"\/items\/{id}\"\uff08\u5b9f\u30d1\u30b9\u3067\u306f\u306a\u304f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\uff09\n    item_id = (event.get(\"pathParameters\") or {}).get(\"id\")\n\n    try:\n        if resource == \"\/items\" and method == \"GET\":\n            return list_items()\n        if resource == \"\/items\" and method == \"POST\":\n            return create_item(parse_body(event))\n        if resource == \"\/items\/{id}\" and method == \"GET\":\n            return get_item(item_id)\n        if resource == \"\/items\/{id}\" and method == \"PUT\":\n            return update_item(item_id, parse_body(event))\n        if resource == \"\/items\/{id}\" and method == \"DELETE\":\n            return delete_item(item_id)\n        return response(405, {\"message\": \"method not allowed\"})\n    except json.JSONDecodeError:\n        return response(400, {\"message\": \"invalid JSON\"})\n    except Exception:\n        # \u8a73\u7d30\u306f CloudWatch Logs \u306b\u3060\u3051\u6b8b\u3057\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u306f\u8fd4\u3055\u306a\u3044\n        logger.exception(\"unhandled error\")\n        return response(500, {\"message\": \"internal server error\"})<\/pre>\n<p>\u30b3\u30fc\u30c9\u3067\u62bc\u3055\u3048\u3066\u304a\u304d\u305f\u3044\u70b9:<\/p>\n<ul>\n<li><code>event[\"resource\"]<\/code>\uff08\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u4e0a\u306e\u30d1\u30b9\uff09\u3067\u5206\u5c90\u3059\u308b\u3068\u3001\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u304c\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n<li>DynamoDB \u306e\u6570\u5024\u306f\u3001\u66f8\u304d\u8fbc\u307f\u6642\u306b <code>parse_float=Decimal<\/code> \u3067\u53d7\u3051\u3001\u8aad\u307f\u51fa\u3057\u6642\u306b <code>default<\/code> \u3067\u6570\u5024\u306b\u623b\u3057\u307e\u3059\u3002<\/li>\n<li>\u5b58\u5728\u30c1\u30a7\u30c3\u30af\u306f <code>ConditionExpression=\"attribute_exists(id)\"<\/code> \u306e\u6761\u4ef6\u4ed8\u304d\u66f8\u304d\u8fbc\u307f\u3067\u884c\u3044\u3001\u5931\u6557\u3057\u305f\u3089 404 \u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n<li><code>name<\/code> \u306f DynamoDB \u306e\u4e88\u7d04\u8a9e\u306a\u306e\u3067\u3001<code>#n<\/code> \u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>4. \u30d3\u30eb\u30c9\u3068\u30c7\u30d7\u30ed\u30a4<\/h3>\n<pre class=\"lang:default decode:true\">sam build\nsam deploy --guided<\/pre>\n<p>\u5bfe\u8a71\u30d7\u30ed\u30f3\u30d7\u30c8\u3078\u306e\u56de\u7b54\u4f8b\u3067\u3059\u3002<\/p>\n<ul>\n<li>Stack Name: <code>items-api<\/code><\/li>\n<li>AWS Region: <code>ap-northeast-1<\/code><\/li>\n<li>Confirm changes before deploy: <code>y<\/code><\/li>\n<li>Allow SAM CLI IAM role creation: <code>Y<\/code><\/li>\n<li>ItemsFunction has no authentication. Is this okay?: \u691c\u8a3c\u7528\u306a\u306e\u3067 <code>y<\/code>\uff08\u30a4\u30d9\u30f3\u30c8\u3054\u3068\u306b\u805e\u304b\u308c\u307e\u3059\uff09<\/li>\n<li>Save arguments to configuration file: <code>Y<\/code><\/li>\n<\/ul>\n<p>\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306f\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">sam list stack-outputs --stack-name items-api --output json<\/pre>\n<h2>\u78ba\u8a8d\u65b9\u6cd5<\/h2>\n<pre class=\"lang:default decode:true\">export API_URL=\"https:\/\/xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com\/dev\"\n\n# \u4f5c\u6210\uff08201\uff09\ncurl -s -X POST \"$API_URL\/items\" \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\"name\": \"keyboard\", \"price\": 12800}' | jq<\/pre>\n<p>\u6b21\u306e\u3088\u3046\u306a JSON \u304c\u8fd4\u308a\u307e\u3059\uff08<code>id<\/code> \u306f\u5b9f\u884c\u3054\u3068\u306b\u5909\u308f\u308a\u307e\u3059\uff09\u3002<\/p>\n<pre class=\"lang:default decode:true\">{\n  \"id\": \"6f1c2c1e-....\",\n  \"name\": \"keyboard\",\n  \"price\": 12800\n}<\/pre>\n<pre class=\"lang:default decode:true\"># \u4e00\u89a7\u30fb1\u4ef6\u53d6\u5f97\ncurl -s \"$API_URL\/items\" | jq\nexport ITEM_ID=\"\uff08\u4f5c\u6210\u6642\u306b\u8fd4\u3063\u305f id\uff09\"\ncurl -s \"$API_URL\/items\/$ITEM_ID\" | jq\n\n# \u66f4\u65b0\uff08\u5c0f\u6570\u3082\u53ef\uff09\ncurl -s -X PUT \"$API_URL\/items\/$ITEM_ID\" \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\"name\": \"keyboard (JIS)\", \"price\": 13800.5}' | jq\n\n# \u524a\u9664\uff08204\uff09\u2192 \u518d\u53d6\u5f97\uff08404\uff09\ncurl -s -o \/dev\/null -w \"%{http_code}\\n\" -X DELETE \"$API_URL\/items\/$ITEM_ID\"\ncurl -s -o \/dev\/null -w \"%{http_code}\\n\" \"$API_URL\/items\/$ITEM_ID\"\n\n# \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc\uff08400\uff09\ncurl -s -X POST \"$API_URL\/items\" -H \"Content-Type: application\/json\" -d '{\"price\": -1}'\n\n# \u30ed\u30b0\u306e\u78ba\u8a8d\nsam logs --stack-name items-api --name ItemsFunction --tail<\/pre>\n<h2>\u3064\u307e\u305a\u304d\u3084\u3059\u3044\u30dd\u30a4\u30f3\u30c8<\/h2>\n<h3>403 <code>Missing Authentication Token<\/code><\/h3>\n<p>\u540d\u524d\u306b\u53cd\u3057\u3066\u3001\u8a8d\u8a3c\u3068\u306f\u95a2\u4fc2\u306a\u304f<strong>\u5b58\u5728\u3057\u306a\u3044\u30d1\u30b9\u30fb\u30e1\u30bd\u30c3\u30c9\u3092\u53e9\u3044\u305f\u3068\u304d<\/strong>\u306b\u51fa\u308b\u3053\u3068\u304c\u591a\u3044\u30a8\u30e9\u30fc\u3067\u3059\u3002URL \u306b\u30b9\u30c6\u30fc\u30b8\u540d\uff08<code>\/dev<\/code>\uff09\u304c\u5165\u3063\u3066\u3044\u308b\u304b\u3001\u30d1\u30b9\u306e\u30b9\u30da\u30eb\u3084\u30e1\u30bd\u30c3\u30c9\u304c\u6b63\u3057\u3044\u304b\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>502 <code>Internal server error<\/code><\/h3>\n<p>Lambda \u306e\u623b\u308a\u5024\u304c\u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u306e\u5f62\u5f0f\u306b\u306a\u3063\u3066\u3044\u306a\u3044\u3068\u51fa\u307e\u3059\u3002<code>statusCode<\/code> \u304c\u3042\u308b\u304b\u3001<code>body<\/code> \u304c\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3044\u308b\u304b\uff08dict \u306e\u307e\u307e\u3060\u3068 NG\uff09\u3001\u672a\u51e6\u7406\u306e\u4f8b\u5916\u304c\u8d77\u304d\u3066\u3044\u306a\u3044\u304b\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>Decimal \/ float \u306e\u30a8\u30e9\u30fc<\/h3>\n<p>boto3 \u306e resource API \u306f DynamoDB \u306e\u6570\u5024\u3092 <code>Decimal<\/code> \u3067\u8fd4\u3059\u305f\u3081\u3001<code>json.dumps<\/code> \u3067\u5909\u63db\u3057\u306a\u3044\u3068\u843d\u3061\u307e\u3059\u3002\u9006\u306b <code>float<\/code> \u306f\u66f8\u304d\u8fbc\u3081\u307e\u305b\u3093\u3002\u8aad\u307f\u66f8\u304d\u306e\u4e21\u65b9\u3067\u5bfe\u51e6\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<h3>CORS \u30a8\u30e9\u30fc<\/h3>\n<p>\u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u3067\u306f\u3001Lambda \u304c\u8fd4\u3059\u30ec\u30b9\u30dd\u30f3\u30b9\u306b <code>Access-Control-Allow-Origin<\/code> \u306a\u3069\u3092\u542b\u3081\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30d7\u30ea\u30d5\u30e9\u30a4\u30c8\uff08<code>OPTIONS<\/code>\uff09\u3078\u306e\u5fdc\u7b54\u306f\u3001<code>AWS::Serverless::Api<\/code> \u306e <code>Cors<\/code> \u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8<\/h3>\n<p>REST API \u306e\u7d71\u5408\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067 29 \u79d2\u3067\u3059\u3002\u91cd\u3044\u51e6\u7406\u306f SQS \u306a\u3069\u3067\u975e\u540c\u671f\u5316\u3057\u307e\u3059\u3002<\/p>\n<h2>\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li><strong>\u8a8d\u53ef<\/strong>\uff1a\u672c\u756a\u3067\u306f Cognito \u30aa\u30fc\u30bd\u30e9\u30a4\u30b6\u30fc\u3001IAM \u8a8d\u53ef\u3001Lambda \u30aa\u30fc\u30bd\u30e9\u30a4\u30b6\u30fc\u306e\u3044\u305a\u308c\u304b\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002API \u30ad\u30fc\u306f\u8a8d\u8a3c\u306e\u4ee3\u308f\u308a\u306b\u306a\u308a\u307e\u305b\u3093\u3002<\/li>\n<li><strong>Scan \u3092\u907f\u3051\u308b<\/strong>\uff1a\u30a2\u30af\u30bb\u30b9\u30d1\u30bf\u30fc\u30f3\u306b\u5408\u308f\u305b\u3066\u30ad\u30fc\u3084 GSI \u3092\u8a2d\u8a08\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30a8\u30e9\u30fc\u8a73\u7d30\u3092\u8fd4\u3055\u306a\u3044<\/strong>\uff1a\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u306f\u30ed\u30b0\u306b\u3060\u3051\u6b8b\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30ed\u30b0\u4fdd\u6301\u671f\u9593<\/strong>\uff1a\u30c7\u30d5\u30a9\u30eb\u30c8\u306f\u7121\u671f\u9650\u3067\u3059\u3002<code>AWS::Logs::LogGroup<\/code> \u3067 <code>RetentionInDays<\/code> \u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u8cbb\u7528<\/strong>\uff1a\u3044\u305a\u308c\u3082\u5f93\u91cf\u8ab2\u91d1\u3067\u3059\u3002\u691c\u8a3c\u74b0\u5883\u3092\u653e\u7f6e\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/li>\n<\/ul>\n<h3>\u7247\u4ed8\u3051<\/h3>\n<pre class=\"lang:default decode:true\">sam delete --stack-name items-api<\/pre>\n<p>API\u30fbLambda\u30fbDynamoDB \u30c6\u30fc\u30d6\u30eb\uff08\u4e2d\u306e\u30c7\u30fc\u30bf\u3082\u542b\u3080\uff09\u304c\u307e\u3068\u3081\u3066\u524a\u9664\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>SAM \u306e <code>Events: Type: Api<\/code> \u3067\u3001\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u30fb\u7d71\u5408\u30fb\u5b9f\u884c\u6a29\u9650\u307e\u3067\u4e00\u62ec\u3067\u5b9a\u7fa9\u3067\u304d\u308b<\/li>\n<li>\u30d7\u30ed\u30ad\u30b7\u7d71\u5408\u306e\u623b\u308a\u5024\u306f\u300c<code>statusCode<\/code> \uff0b \u6587\u5b57\u5217\u306e <code>body<\/code>\u300d\u3002\u5d29\u308c\u308b\u3068 502 \u306b\u306a\u308b<\/li>\n<li>Python \u00d7 DynamoDB \u3067\u306f <code>Decimal<\/code> \u306e\u6271\u3044\u3092\u6700\u521d\u306b\u6c7a\u3081\u308b<\/li>\n<li>403 <code>Missing Authentication Token<\/code> \u306f\u3001\u307e\u305a\u30d1\u30b9\u3068\u30b9\u30c6\u30fc\u30b8\u540d\u3092\u7591\u3046<\/li>\n<li>REST API \u3068 HTTP API \u306f\u8981\u4ef6\u3067\u9078\u3076<\/li>\n<\/ul>\n<p>\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u3068\u3057\u3066\u306f\u3001Cognito \u30aa\u30fc\u30bd\u30e9\u30a4\u30b6\u30fc\u306b\u3088\u308b\u8a8d\u53ef\u3001Powertools for AWS Lambda\uff08Python\uff09\u3067\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3068\u69cb\u9020\u5316\u30ed\u30b0\u3001GitHub Actions \u304b\u3089\u306e <code>sam deploy<\/code> \u81ea\u52d5\u5316\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Amazon API Gateway\uff08REST API\uff09\u3068 AWS Lambda\u3001DynamoDB \u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u30a2\u30a4\u30c6\u30e0\u3092 CRUD \u3059\u308b\u5c0f\u3055\u306a REST API \u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":3890,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","_lightning_design_setting":[],"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[75],"tags":[64,499,1176,670,1080,1193,1081,147,562,750],"class_list":["post-3891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code-of-serverside","tag-api","tag-aws","tag-cognito","tag-curl","tag-dynamodb","tag-iam","tag-lambda","tag-python","tag-562","tag-750"],"veu_head_title_object":{"title":"","add_site_title":""},"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2026\/11\/20261119-plan123-thumbnail.jpg","_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=3891"}],"version-history":[{"count":1,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3891\/revisions"}],"predecessor-version":[{"id":3893,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/3891\/revisions\/3893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/3890"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=3891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=3891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=3891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}