4

I am creating a AWS Lambda function using Python. I want to create S3 bucket but I am getting error as the bucket name provided by me is not JSON serializable.

Here is the code I used for creating a bucket with Lambda:

import boto from boto 
import s3 from boto.s3.connection 
import S3Connection

def lambda_handler(event, context):
  conn = S3Connection('access_key','secret_access_key')
  print "Connection:",conn
  bucket = conn.create_bucket('bucketname')
  print bucket
  return bucket
4
  • Please provide the code you are using and the error message you are experiencing. For tips on asking a good question, please see: How do I ask a good question? Commented Jul 22, 2017 at 1:10
  • Please find below code which I used for creating bucket with Lambda:- import boto from boto import s3 from boto.s3.connection import S3Connection def lambda_handler(event, context): conn = S3Connection('access_key','secret_access_key') print "Connection:",conn bucket = conn.create_bucket('bucketname') print bucket return bucket Commented Jul 23, 2017 at 15:25
  • Feel free to Edit your question rather than adding code in a comment. Within a Lambda function, there is no need to obtain an Access Key and Secret Key. It is automatically provided as a result of assigning a Role to the Lambda function. Did you Lambda function work? What error did you receive? You can view the log in Amazon CloudWatch Logs. Commented Jul 23, 2017 at 22:30
  • Please show the exception error. Commented Jul 25, 2017 at 14:56

1 Answer 1

3

From Creating and Using Amazon S3 Buckets boto3 documentation:

import boto3

s3 = boto3.client('s3')
s3.create_bucket(Bucket='my-bucket')

Rules for bucket names:

  • The bucket name can be between 3 and 63 characters long, and can contain only lower-case characters, numbers, periods, and dashes.
  • Each label in the bucket name must start with a lowercase letter or number.
  • The bucket name cannot contain underscores, end with a dash, have consecutive periods, or use dashes adjacent to periods.
  • The bucket name cannot be formatted as an IP address (198.51.100.24).
Sign up to request clarification or add additional context in comments.

2 Comments

I followed all the rules for S3 bucket.
this worked for me s3 = boto3.resource('s3') b = s3.Bucket('laddu47') b.create(CreateBucketConfiguration={ 'LocationConstraint': 'ap-south-1' })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.