我在aws ec2实例中部署了我的应用程序,并且正确地设置了vpc窥视和ip白名单,一切都很好。应用程序正在运行,我能够访问我的mongodb地图集数据库。
然后我试图在无服务器环境下部署我的应用程序,我无法访问我的mongodb地图集数据库。我在aws中使用了与我的ec2服务器使用的配置相同的配置。奇怪的是,当我在本地运行我的应用程序时,我可以访问数据库。
我遗漏了什么吗?这是我的配置
# index.js
const serverless = require('serverless-http');
const express = require('express')
const app = express()
const MongoClient = require('mongodb').MongoClient
app.get('/users', async (req, res) => {
const url = 'mongodb+srv://<username>:<password>@<cluster>/<dbname>?retryWrites=true'
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, db) {
if (err) throw err;
console.log(err)
var dbo = db.db("<dbName>");
dbo.collection("users").find({}).toArray((err, data) => {
if (err) throw err
res.send(data)
})
});
})
module.exports.handler = serverless(app)#serverless.yml
service: gmt-api
custom:
serverless-offline:
port: 3000
provider:
name: aws
runtime: nodejs12.x
stage: ${opt:stage, 'development'}
region: ap-southeast-1
stackName: ${self:service}-${self:provider.stage}-api
endpointType: regional
environment:
NODE_ENV: ${self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- ec2:CreateNetworkInterface
- ec2:DeleteNetworkInterface
- ec2:DescribeNetworkInterfaces
Resource: "*"
- Effect: Allow
Action:
- elasticache:*
Resource: "*"
- Effect: Allow
Action:
- s3:*
Resource: "*"
vpc:
securityGroupIds:
- <security group id>
subnetIds:
- <subnet 1>
- <subnet 2>
- <subnet 3>
functions:
app:
wampup: true
handler: index.handler
name: ${self:service}-${opt:stage, self:provider.stage}-serverless
events:
- http: ANY /
- http: 'ANY {proxy+}'
cors:
origin: '*'
maxAge: 86400
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
allowCredentials: false
cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate'
plugins:
- serverless-offline
- serverless-plugin-warmup发布于 2020-08-04 07:40:58
https://stackoverflow.com/questions/63242028
复制相似问题