2

Q: Can I use Ansible in order to build/deploy an AWS SAM (Serverless Application Model) template? If so can anyone post an example of how this Ansible Playbook (e.g. with build/deploy tasks) should look like?

Note:

I've searched the web and haven't found anyone giving such an example. AWS SAM is not even mentioned in Ansible documentation. except a GitHub issue (feature idea) that suggests supporting SAM with Ansible - but that was from 2017.

Thanks a lot

Mercury
  • 7,430
  • 3
  • 42
  • 54
  • The answer will differ based on whether you mean build as a task, deploy as a task, or build and then deploy as one task. The answer is also ultimately "automate whatever steps you would type by hand" since computers are deterministic that way – mdaniel May 21 '21 at 15:34
  • I don't care if build deploy will be in separate tasks or not, just want to know if ansible can deploy AWS Cloud Formation stack based on SAM template, and some example that demonstrates this will be great. – Mercury May 22 '21 at 20:49
  • https://www.ansible.com/resources/videos/ansible-aws-automate-serverless-application-deploys-with-ansible? – β.εηοιτ.βε May 23 '21 at 12:09
  • Thanks but not what I am looking for, Seen this video, and this is how to create services in AWS using Ansible, BUT not by using CF / SAM templates, I want to create a CF Stack using SAM templates. – Mercury May 23 '21 at 18:48

1 Answers1

0

Yes sure you can.. here is the Ansible reference help and another help article


get the Serverless Plugin, help here

..."This plugin is part of the community.general collection (version 3.0.2).

To install it use: ansible-galaxy collection install community.general..."


- name: Basic deploy of a service
  community.general.serverless:
    service_path: '{{ project_dir }}'
    state: present

- name: Deploy specific functions
  community.general.serverless:
    service_path: '{{ project_dir }}'
    functions:
      - my_func_one
      - my_func_two

- name: Deploy a project, then pull its resource list back into Ansible
  community.general.serverless:
    stage: dev
    region: us-east-1
    service_path: '{{ project_dir }}'
  register: sls

# The cloudformation stack is always named the same as the full service, so the
# cloudformation_info module can get a full list of the stack resources, as
# well as stack events and outputs
- cloudformation_info:
    region: us-east-1
    stack_name: '{{ sls.service_name }}'
    stack_resources: true

- name: Deploy a project using a locally installed serverless binary
  community.general.serverless:
    stage: dev
    region: us-east-1
    service_path: '{{ project_dir }}'
    serverless_bin_path: node_modules/.bin/serverless
Transformer
  • 6,963
  • 2
  • 26
  • 52