Developing software in the Real World

My Bref Makefile

In order to use Bref efficiently, I’ve developed a Makefile so that I don’t have to remember all the various commands required. In particular, looking up the correct parameters to sam package & sam deploy is a pain and it’s much easier to type make deploy and it all works as I expect.

It looks like this:

Makefile:

There’s three variables that I need to set at the top:

  • REGION – The AWS region. This has to match the Bref layer used in template.yaml.
  • PROJECT_NAME – The name of the project. This is used as part of the S3 bucket and CloudFormation stack names
  • UNIQUE_KEY – A random string to ensure uniqueness for bucket and stack names. I tend to use the current time to the ms, but any string.

I’ve included a full-cycle set of targets so make setup will create the initial S3 bucket that’s required for the project and then make deploy is used to deploy my project.

If I want to start again, make clean will remove the CloudFormation stack and make clean-all will remove the stack and the bucket.

I’ve also included a few utility targets:

  • make invoke FUNCTION=foo invokes the function foo on AWS.
  • make invoke-local FUNCTION=foo invokes the function foo on sam-local.
  • make outputs displays the outputs of the CloudFormation stack. This is useful for picking up the API Gateway URL for instance, if you set it up in your template.yaml.
  • make lastlog FUNCTION=foo displays the logs for the last invocation of the function foo.

Parameters for template.yaml

I pass the PROJECT_NAME and UNIQUE_KEY through to the template as the parameters ProjectName and UniqueKey respectively. These are then set in the Parameters section of the template:

template.yaml:

I then use them in the template when I need uniqueness, such as when creating an S3 bucket:

template.yaml:

Which creates a bucket named “hello-world-1557903576-files” which nicely complements “hello-world-1557903576-brefapp”.

2 thoughts on “My Bref Makefile

  1. Hi Rob – thanks for writing this up.

    Can you explain why you went with a makefile instead of one or more shell scripts? I see this a lot recently – using make when there are no dependency trees – and don't understand the appeal.

    1. Greg,

      Mostly for consistency. I write projects in different languages and different systems and I've found make to integrate well with my editor. There's no reason that this couldn't be a set of composer scripts for instance.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *