AWS Lambda function URLsを手軽に実行

# 世間の方々よりも少し遅いけど、AWS Lambda function URLs を試してみよう

- ドキュメント
    - https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html


## AWS マネジメントコンソール Lambda から

- マネジメントコンソールで操作
![02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/6a9ecc1c-598a-982b-429e-f58e5bc4f419.png)

- 「関数の作成」ボタンを押す
- 作成された内容を確認 ,URLが払い出しされて紐づいている
![03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/2c2b3c50-6420-daab-8f78-f68fd38bfa29.png)

作成された URL:https://ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws/

GETアクセスして、
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/a1b6114a-9b47-4056-8aec-ad565f90f9ee.png)

- 関数の中身を変更してみる
- event をログ出力する

```js
exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify(event, null, 2)
    };
    return response;
};
```

- deploy ボタンを押してURlアクセス

- ブラウザに表示された内容
```
{
  "version": "2.0",
  "routeKey": "$default",
  "rawPath": "/",
  "rawQueryString": "",
  "headers": {
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "cross-site",
    "accept-language": "ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7",
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "x-forwarded-for": "xxx.xx.xx.xx",
    "sec-fetch-user": "?1",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
    "sec-ch-ua-mobile": "?0",
    "x-amzn-trace-id": "Root=1-62598bcb-69680d4b1cba0ce16f1421c3",
    "sec-ch-ua-platform": "\"Windows\"",
    "host": "ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws",
    "upgrade-insecure-requests": "1",
    "cache-control": "max-age=0",
    "accept-encoding": "gzip, deflate, br",
    "sec-fetch-dest": "document",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
  },
  "requestContext": {
    "accountId": "anonymous",
    "apiId": "ej2mqhfzpd45oazcipkwzihvya0iegzm",
    "domainName": "ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws",
    "domainPrefix": "ej2mqhfzpd45oazcipkwzihvya0iegzm",
    "http": {
      "method": "GET",
      "path": "/",
      "protocol": "HTTP/1.1",
      "sourceIp": "xx.xx.xx.xx",
      "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
    },
    "requestId": "90d3ce44-7c41-4b63-bc55-704b4e8b6e11",
    "routeKey": "$default",
    "stage": "$default",
    "time": "15/Apr/2022:15:14:19 +0000",
    "timeEpoch": 1650035659266
  },
  "isBase64Encoded": false
}
```

- `https://ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws/foor/bar/` URLのドメイン以降に付加してみる

```
{
  "version": "2.0",
  "routeKey": "$default",
  "rawPath": "/foor/bar",
  "rawQueryString": "",
  "headers": {
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "accept-language": "ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7",
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "x-forwarded-for": "xx.xx.xx.xx",
    "sec-fetch-user": "?1",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
    "sec-ch-ua-mobile": "?0",
    "x-amzn-trace-id": "Root=1-62598c07-71b759e8321e5c3b6658ec32",
    "sec-ch-ua-platform": "\"Windows\"",
    "host": "ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws",
    "upgrade-insecure-requests": "1",
    "accept-encoding": "gzip, deflate, br",
    "sec-fetch-dest": "document",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
  },
  "requestContext": {
    "accountId": "anonymous",
    "apiId": "ej2mqhfzpd45oazcipkwzihvya0iegzm",
    "domainName": "ej2mqhfzpd45oazcipkwzihvya0iegzm.lambda-url.us-west-2.on.aws",
    "domainPrefix": "ej2mqhfzpd45oazcipkwzihvya0iegzm",
    "http": {
      "method": "GET",
      "path": "/foor/bar",
      "protocol": "HTTP/1.1",
      "sourceIp": "xxx.xx.xx.xx",
      "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
    },
    "requestId": "2eb6db32-e31f-4302-ad56-8c898e66628d",
    "routeKey": "$default",
    "stage": "$default",
    "time": "15/Apr/2022:15:15:19 +0000",
    "timeEpoch": 1650035719215
  },
  "isBase64Encoded": false
}
```

```
"rawPath": "/foor/bar",
```
アクセスされた内容が取得できる、GET相当が取得できる

最後に、誰でも認証なく実行できるのでLambdaを消しておく
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1482752/e65d4716-de35-7f64-c0a4-f69f93288df7.png)