{"id":19208629,"url":"https://github.com/heiwa4126/sam-cors1","last_synced_at":"2026-05-19T07:03:55.906Z","repository":{"id":111910693,"uuid":"418799463","full_name":"heiwa4126/sam-cors1","owner":"heiwa4126","description":"CORS(Cross-Origin Resource Sharing)のテスト環境をAWS SAMで展開する。","archived":false,"fork":false,"pushed_at":"2021-12-27T04:49:51.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-04T16:18:56.515Z","etag":null,"topics":["aws","cloudformation","cors","sam"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heiwa4126.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-19T06:33:02.000Z","updated_at":"2021-12-27T04:49:54.000Z","dependencies_parsed_at":"2023-04-07T22:14:48.739Z","dependency_job_id":null,"html_url":"https://github.com/heiwa4126/sam-cors1","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heiwa4126%2Fsam-cors1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heiwa4126%2Fsam-cors1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heiwa4126%2Fsam-cors1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heiwa4126%2Fsam-cors1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heiwa4126","download_url":"https://codeload.github.com/heiwa4126/sam-cors1/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240280984,"owners_count":19776418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","cloudformation","cors","sam"],"created_at":"2024-11-09T13:27:13.809Z","updated_at":"2025-10-20T01:40:44.387Z","avatar_url":"https://github.com/heiwa4126.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sam-cors1\n\nCORS(Cross-Origin Resource Sharing)のテスト環境をAWS SAMで展開します。\n\nデプロイがめんどうな人は\n\n* [1.tmplate.js](1.tmplate.js)\n* [hello/app.py](hello/app.py)\n\nだけでも参考になると思います。\n\n# 目次\n\n- [sam-cors1](#sam-cors1)\n- [目次](#目次)\n- [デプロイに必要なもの](#デプロイに必要なもの)\n- [デプロイ](#デプロイ)\n- [テスト](#テスト)\n  - [サンプルlambdaのメモ](#サンプルlambdaのメモ)\n- [削除](#削除)\n- [CROSメモ](#crosメモ)\n\n\n# デプロイに必要なもの\n\n* [AWS CLI](https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2.html)\n* [AWS SAM CLI](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/serverless-sam-cli-install-linux.html)\n* Python 3.8\n* [jq](https://stedolan.github.io/jq/download/)\n* [yq](https://github.com/kislyuk/yq)\n\n\n# デプロイ\n\nまずSAMなので\n\n```sh\nsam build\nsam deploy --guided  # 最初の1回。2回目以降は `sam deploy`\n```\nで、サンプルのlambdaとサンプルのWWWコンテンツを置くS3を展開します。\n\n`sam deploy --guided`では以下の2つを除いてデフォルトでOK。\n\n* `Parameter BucketName` は S3のバケット名なので、独自の名前をつけてください。\n* `HelloFunction may not have authorization defined, Is this okay? [y/N]:` は `y`で。\n\n無事スタックがデプロイされたら、Outputの\n`S3URL` と `S3SecureURL` の値をメモしてください\n(忘れてもポータルのCloudFormationの該当スタックの出力から見れます)。\n\n次に、\n\n```sh\n./update_1js.sh\n```\nで、`1.template.js` から `contents/js/1.js` を作成\n(lambdaのURLを埋め込んでいます)。\n\n最後に\n```sh\n./sync_contents.sh\n```\nでcontents/ 以下をS3に転送します。\n\n\n# テスト\n\nOutputの `S3URL` か `S3SecureURL` のURLに\nブラウザからアクセスしてみてください。\n\njQueryの$.ajaxを使って、かんたんなクロスドメインのテストが実行されます。\n\n## サンプルlambdaのメモ\n\nlambda APIの戻り値は以下のようなものです。\n\n```json\n{\"message\": \"OK\", \"time\": \"2021-10-18 07:33:47.955417+00:00\"}\n```\n\n* message - `OK`で固定\n* time - 現在時刻をUTCで\n\njs/1.jsではこのうちtimeを表示します。\n\n\n# 削除\n\n実験が終わったら\n```sh\n./delete_stack.sh\n```\nで、S3の中身とスタックを削除してください。\n\n\n# CROSメモ\n\nSAM(CFn)ではOPTIONSを作ってくれますが、\n個々のlambdaでも\nAccess-Control-Allow-* 3つ\nを返す必要があります。\n\nいまのCFnテンプレートの設定だと\n全部のAPIに同じ設定のCORSのOPTIONSがついてしまう。\nAWS::Serverless::FunctionのPropertiesにはCorsがないので\n個別で指定することはできないみたい。\n\n\nAccess-Control-Allow-Originは\n\n* null\n* `*`\n* オリジン1つ\n\nの、いずれかが指定できます。\n**「スペースで区切ってオリジンを複数指定」**\nは出来ません。\n\nOPTIONSの返すAccess-Control-Allow-* と\nGET,PUTの返すそれは同じである必要はありません\n(Preflightの場合AND条件になるみたい)。\nCFnだと、プリフライトのOPTIONSの返すAccess-Control-Allow-*はゆるめに、\n個々のAPIではきつめにせざるおえないみたい。\n\nオリジンはドメインとはちがいます。\nオリジンはスキーマとドメイン、ポートはオプション。\n\n正しいオリジン例:\n- http://www.example.com\n- https://www.example.com\n- http://www.example.com:8080\n\n正しくないオリジン例:\n- www.example.com\n- http://www.example.com/\n\n# 参考\n\n* [CorsConfiguration - AWS Serverless Application Model](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-api-corsconfiguration.html)\n* [Access-Control-Allow-Origin に設定する値として\"マシ\"なのはどちらか - SSTエンジニアブログ](https://techblog.securesky-tech.com/entry/2021/12/09/)\n* [Access-Control-Allow-Credentials - HTTP | MDN](https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheiwa4126%2Fsam-cors1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheiwa4126%2Fsam-cors1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheiwa4126%2Fsam-cors1/lists"}