DEV Community

Simplifying Infrastructure Deployment with Sceptre: Your First Stack — An S3 Bucket

Creating an S3 bucket using Sceptre

Introduction

In this article, we’ll walk you through creating and deploying your first stack using Sceptre, with a simple example of creating an S3 bucket.

Prerequisites

Before we begin, make sure you have gone through the article on A Guide to Installing and Configuring Sceptre

Building Infrastructure — The S3 Bucket

The Directory Structure

1. Defining the S3 Bucket Config — s3-bucket-config.yaml

 template:
      path: s3-bucket-template.yaml
      type: file

    stack_name: my-s3-bucket-stack

    parameters:
      bucketname: first-secptre-bucket-20230727
      deletionpolicy: Delete
Enter fullscreen mode Exit fullscreen mode

This Sceptre config file creates an S3 bucket using the specified CloudFormation template file (s3-bucket-template.yaml) with the given parameters. The S3 bucket's name will be "my-first-sceptre-bucket-20230727," and it will have the "Delete" deletion policy.

Below is the explanation of each section in the config file:

  • path: s3-bucket-template.yaml: This line specifies the path to the CloudFormation template file (s3-bucket-template.yaml) relative to the Sceptre configuration file’s location. The template file contains the CloudFormation code that defines the S3 bucket’s properties. The path property may consist of either an absolute or relative path to the template file. When using a relative path, this handler assumes it is relative to the ‘sceptre_project_dir/templates’ directory

  • type: file: This indicates the type of the template, which in this case is a file. This is one of the possible values for the type property, and it is used to indicate the external source location of the template.(e.g., file,s3,http).

In the provided example, the CloudFormation template is stored in an S3 bucket, and its path is provided using the path attribute. Sceptre will retrieve the template from the specified S3 bucket and use it to create the stack.

template:
  path: s3://my-bucket/s3-bucket-template.yaml
  type: s3
Enter fullscreen mode Exit fullscreen mode
  • stack_name: my-s3-bucket-stack: This line defines the name of the CloudFormation stack that will be created. In this case, the stack will be named “my-s3-bucket-stack.”

  • parameters: This section defines the input parameters that will be passed to the CloudFormation stack during its creation. These parameters customize the properties of the S3 bucket being created. The parameters listed are:
    – bucketname: This parameter is set to “my-first-sceptre-bucket- 20230727,” which specifies the desired name for the S3 bucket.
    – deletionpolicy: This parameter is set to “Delete,” which indicates the desired deletion policy for the S3 bucket. In this case, the bucket will be deleted when the CloudFormation stack is deleted

2. Defining the S3 Bucket Template— s3-bucket-template.yaml

Description: Template creates an empty S3 Bucket

    Parameters:
      bucketname:
        Type: String

      deletionpolicy:
        Type: String
        Default : Retain
        AllowedValues:
          - Delete
          - Retain

    Resources:
      MyS3Bucket: 
        DeletionPolicy: !Ref deletionpolicy
        Type: 'AWS::S3::Bucket'
        Properties:
          BucketName: !Ref bucketname

    Outputs: 
      S3BucketName: 
        Value: !Ref MyS3Bucket 
        Description: Name of the S3 Bucket 
      S3BucketARN: 
        Value: !GetAtt MyS3Bucket.Arn 
        Description: ARN of the Bucket
Enter fullscreen mode Exit fullscreen mode

The above template allows users to create an S3 bucket with a custom name and specify the desired deletion policy. By using the outputs, users can easily access and utilize the bucket name and ARN for further operations and integration within their AWS environment.

Let’s break down each section of the template:

  1. Parameters : This section defines the input parameters that can be provided when launching the CloudFormation stack. In this template, there are two parameters.

  2. Resources : This section defines the AWS resources that the CloudFormation stack will create. In this template, there is one resource. How to specify an S3 bucket in CloudFormation

  3. Outputs : This section defines the information that will be displayed once the CloudFormation stack is created. In this template, there are two outputs.

3. Different Sceptre commands for deploying and managing the stacks

Before executing the Sceptre commands, ensure that you change the current working directory to the project directory. In our scenario, the project directory is named “first-sceptre-directory.”

  • Validating the stack : You can verify that your CloudFormation templates are well-formed, follow the correct YAML or JSON syntax, and reference valid AWS resources and properties.

    sceptre validate s3-bucket-config.yaml

Using Docker

docker run -v "$(pwd):/project"  -v "$HOME/.aws:/root/.aws" -w /project sceptre-image validate s3-bucket-config.yaml
Enter fullscreen mode Exit fullscreen mode

This Docker command creates a container based on the “sceptre-image” Docker image, maps the current working directory and AWS configuration from the host to appropriate directories inside the container, sets the working directory for the Sceptre command to “/project,” and then runs the Sceptre “validate” command on the “s3-bucket-config.yaml” configuration file.

  • Creating the stack: This command will initiate the stack creation process using the associated CloudFormation template.
    sceptre validate s3-bucket-config.yaml

    sceptre create s3-bucket-config.yaml

Using Docker

docker run -v "$(pwd):/project"  -v "$HOME/.aws:/root/.aws" -w /project sceptre-image create s3-bucket-config.yaml
Enter fullscreen mode Exit fullscreen mode

  • Updating the stack: This command will trigger the update process, and CloudFormation will apply the changes specified in the template to the existing stack.

    sceptre update s3-bucket-config.yaml

Using Docker

docker run -v "$(pwd):/project"  -v "$HOME/.aws:/root/.aws" -w /project sceptre-image update s3-bucket-config.yaml
Enter fullscreen mode Exit fullscreen mode

After the deployment is successful, go to the AWS Cloudformation console and verify the creation of your stack and S3 bucket with the specified name (my-first-sceptre-bucket in our example).

Stack is in create-complete state

S3 Bucket resource in the stack

Parameters passed to the stack

Outputs defined in the stack

  • Deleting the stack: This command will delete the CloudFormation stack and the associated resources.

    sceptre delete s3-bucket-config.yaml

Using Docker

docker run -v "$(pwd):/project"  -v "$HOME/.aws:/root/.aws" -w /project sceptre-image delete s3-bucket-config.yaml
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You have successfully created and deployed your first stack using Sceptre.

This simple example of creating an S3 bucket serves as a foundation for more complex deployments and scenarios. As you explore additional features and customization options, refer to the official Sceptre documentation for a deeper understanding.

Leveraging Sceptre, you can streamline infrastructure management and elevate your AWS cloud deployment experience. Happy deploying!

Top comments (0)