Showing posts with label S3. Show all posts
Showing posts with label S3. Show all posts

Tuesday, March 7, 2023

Ansible playbook for AWS S3 bucket creation | How to create S3 bucket using Ansible in AWS Cloud

We will learn how to create new S3 bucket using Ansible playbook and automate the execution using Jenkins Pipeline. 


Pre-requisites:


  • Playbook for creating new S3 bucket needs to be created but you can refer my GitHub Repo

Ansible playbook for AWS S3 bucket creation

Steps:

1. Create Ansible playbook for S3 bucket creation

(Sample playbook is available in my GitHub Repo, you can use that as a reference)

2. Create Jenkins Pipeline 

pipeline {
    agent any
    stages {
        
        stage ("checkout") {
            steps {
                        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],                                                     userRemoteConfigs: [[url: 'https://github.com/akannan1087/myAnsibleInfraRepo']]])         
            }
        }
        stage('execute') {
            steps {
                //to suppress warnings when you execute playbook    
                sh "pip install --upgrade requests==2.20.1"
                // execute ansible playbook
                ansiblePlaybook 'create-s3.yml'
            }
        }
    }
}

Execute Pipeline


Pipeline Console output


Playbook for creating S3 for your reference:

create-s3.yml

---
 - name:  provisioning S3 Bucket using Ansible playbook
   hosts: localhost
   connection: local
   gather_facts: False
   tags: provisioning

   tasks:
     - name: create S3 bucket
       s3_bucket:
         name: myansibles3bucket312
         state: present
         region: us-east-1
         versioning: yes
         tags:
           name: myansiblebucket
           type: example
       register: s3_url

     - name: Display s3 url
       debug: var=s3_url

Sunday, December 16, 2018

How to create S3 bucket in AWS using Terraform - Create S3 bucket in AWS using Terraform

Terraform is an infrastructure orchestration tool for creating web services in AWS automatically. You can use Terraform for provisioning S3 bucket in AWS.

sudo vi create_s3.tf

resource "aws_s3_bucket" "mybucket" {
  bucket = "my-tf-test-bucket"
  acl    = "public-read"
  website {
    index_document = "hello.html"
   routing_rules = <<EOF
[{
    "Condition": {
        "KeyPrefixEquals": "docs/"
    },
    "Redirect": {
        "ReplaceKeyPrefixWith": "documents/"
    }
}]
EOF
  }

  tags= {
    Name        = "My bucket"
    Environment = "Dev"
  }
}

Once you create the above file, execute terraform plan and then terraform apply to create S3 bucket in AWS.