Elastic Beanstalk无法连接DynamoDB

时间:2016-09-18 16:22:13

标签: amazon-web-services amazon-ec2 amazon-dynamodb elastic-beanstalk aws-php-sdk

我非常喜欢aws,如果这是一个愚蠢的问题,那就很抱歉。

我做了一个连接在线DynamoDB表的网站,它完全在我的本地工作,但是当我在Elastic Beanstalk环境中部署网站代码时,它没有连接到DynamoDB表。

我应该做一些配置吗?

我担心没有其他人有这个问题。

我的网站是由aws.phar使用aws php sdk编写的。

我确实为iam用户创建了一个策略,如下所示:

    public void Connect(String server, String message)
    {
        try
        {                
            TcpClient client = new TcpClient(server, 1400);

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // Get a client stream for reading and writing.
            //  Stream stream = client.GetStream();

            NetworkStream stream = client.GetStream();

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            //Console.WriteLine("Sent: {0}", message);

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            //Console.WriteLine("Received: {0}", responseData);

            // Close everything.
            stream.Close();
            client.Close();
        }
        catch (ArgumentNullException e)
        {
            //Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
          //  Console.WriteLine("SocketException: {0}", e);
        }

        //Console.WriteLine("\n Press Enter to continue...");
        //Console.Read();
    }

2 个答案:

答案 0 :(得分:4)

通过将Elastic Beanstalk自动创建的角色的正确权限添加到EC2实例来解决此问题。

http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html

答案 1 :(得分:0)

当我在代码中包含有关区域,访问密钥和秘密密钥的详细信息时,它对我有用。我替换了

dynamodb = boto3.resource('dynamodb')

使用

dynamodb = boto3.resource('dynamodb', region_name = Region_NAME, 
    aws_access_key_id = ACCESS_KEY, aws_secret_access_key =
    SECRET_ACCESS_KEY)

尽管这不是推荐的解决方案,但是由于安全性问题,您不应该在代码中包括凭据。