@kiku_26

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[AWS]外部APIを使用するlambdaのテストのために、プライベートDNSを使ってVPC内のlambdaを呼び出したい

解決したいこと

AWSで、外部REST APIサービスを使うlambdaを作成しています。
製造中、テストにおいて、本物のAPIを呼ばずに、内部で作成したlambaで擬似レスポンスを返したいと思っています。
テストしたいソースには、変更を加えずテストしたいので、
PrivateDNS → API Gateway → lambda
のような構成にできればと思っています。
色々な情報から、
Route53に、実際に呼び出すドメインを登録し、Aレコードを作成しました。
名前解決はできているようなのですが、結果は
403になってしまいます。
実現の方法をご存知の方がいたら、教えてください!

発生している問題・エラー

403:forbidden

該当するソースコード(呼び出し側)

Node.js
const https = require('https')

exports.handler = async (event) => {
    const api_url = 'https://www.jma.go.jp/bosai/common/const/area.json'
    const hostname = new URL(api_url).hostname;
    const path = new URL(api_url).pathname + new URL(api_url).search;
    const options = {
        hostname: hostname,
        port: 443,
        path: path,
        method: 'POST',
        rejectUnauthorized: false
      };
    const res = await req(options)
        .catch(err => err)
    console.log(res);
    return {
        api_url,
        res
    }
}

async function req (options) {
    return new Promise((resolve, reject) => {
        https.get(options, (resp) => {
            let data = ''
            resp.on('data', (chunk) => {
                data += chunk
            })
            resp.on('end', () => {
                resolve(data)
            })
        })
        .on('error', (err) => {
          reject(err)
        })
    })
}

受信側
レスポンスをJSONで返すだけのソース

自分で試したこと

・直接API Gateway経由(ARNを指定した呼び出し)は、OK
・外部URLを指定した時、プライベートDNSからは、設定した内部IPが結果として返って来ている(Route53のログで確認済み)
・ただし、API Gatewayのログは出力されておらず、403エラーが返ってくる。

0 likes

No Answers yet.

Your answer might help someone💌