RAcharya
RAcharya

Reputation: 45

How to create S3 bucket using AWS Lambda python?

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

Upvotes: 4

Views: 7287

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269091

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).

Upvotes: 3

Related Questions