DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Keep Your Application Secrets Secret
  • Container Checkpointing in Kubernetes With a Custom API
  • Leveraging Seekable OCI: AWS Fargate for Containerized Microservices
  • Deploying Dockerized Applications on AWS Lambda: A Step-by-Step Guide

Trending

  • Designing AI Multi-Agent Systems in Java
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Serverless Asterisk with Docker and AWS Fargate

Serverless Asterisk with Docker and AWS Fargate

Take a look at how to use freebase tool Asterisk with Docker and AWS Fargate that requiresl minimal maintenance and administration.

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
Sep. 23, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

Asterisk is a powerful freebase PBX providing VoIP and Telephony solutions, catering to the needs of both Enterprise and Stand-Alone levels. In the article below, we would demonstrate the creation of a highly-scalable Asterisk cloud server through Fargate task, which would require minimal maintenance and administration.

Prerequisite

  1. Docker installed on your personal machine.
  2. An active AWS account for deploying images.
    Note: You can use either of AWS EC2 or Cloud9 IDE for setting up Docker

Creating Asterisk on Docker

1. Creating the Base Image

Shell
 




x


 
1
#base image for container
2
FROM debian:buster-slim


We have chosen Debian Buster Slim for the base image as shown above. It is recommended that you keep the container light and use smallest size for the base image. You can also choose Alpine, Jessie or Stretch versions as per your convenience.

2. Setting Up Asterisk

Shell
 




xxxxxxxxxx
1


 
1
#base image for container
2
$ docker build -t asterisk:latest 
3
Sending build context to Docker daemon  2.048kBStep 1/1 : FROM debian:buster-slim: Pulling from library/debian8ec398bc0356: Pull completeDigest: sha256:e4c1417236abc57971755ca2bfccd546cbca45b33daf66001a5addae4bf78517Status: Downloaded newer image for debian:buster-slim---> e1af56d072b8Successfully built e1af56d072b8Successfully tagged asterisk:latest


Use docker build -t asterisk to setup Asterisk on the Docker image.

Shell
 




xxxxxxxxxx
1


 
1
$docker images
2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
3
<none>              <none>              a8b00d3e8229        37 minutes ago      592MB
4
asterisk            latest              e1af56d072b8        2 weeks ago         69.2MB
5
debian              buster-slim         e1af56d072b8        2 weeks ago         69.2MB
6
debian              latest              b5d2d9b1597b        2 weeks ago         114MB


As shown above, Asterisk is installed with image id e1af56d072b8 showing the image size of 69.2 MB which is tiny compared to the overall ISO size.

3. Labeling the Asterisk Image

Shell
 




xxxxxxxxxx
1


 
1
#base image for container
2
FROM debian:buster-slim
3

          
4
LABEL maintainer="Bora Ozkan <boraozkan@gmail.com>"


Once the labeling is done, you can run docker inspect to do a quick inspection of the Docker image as shown below.

Shell
 




xxxxxxxxxx
1
92


 
1
$docker inspect asterisk:latest 
2
[
3
    {
4
        "Id": "sha256:d216cbd1c686b8afbcaab3406adf02a8ac481797510ea5a189ac1314ae654ead",
5
        "RepoTags": [
6
            "asterisk:latest"
7
        ],
8
        "RepoDigests": [],
9
        "Parent": "sha256:e1af56d072b8d93fce4b566f4bf76311108dbbbe952b12a85418bd32c2fcdda7",
10
        "Comment": "",
11
        "Created": "2020-01-14T09:06:47.708064046Z",
12
        "Container": "d552618057cb929857b2c9dbd1efa2f2be9d7e422c7ab85ff858fa2064398cb1",
13
        "ContainerConfig": {
14
            "Hostname": "d552618057cb",
15
            "Domainname": "",
16
            "User": "",
17
            "AttachStdin": false,
18
            "AttachStdout": false,
19
            "AttachStderr": false,
20
            "Tty": false,
21
            "OpenStdin": false,
22
            "StdinOnce": false,
23
            "Env": [
24
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
25
            ],
26
            "Cmd": [
27
                "/bin/sh",
28
                "-c",
29
                "#(nop) ",
30
                "LABEL maintainer=Bora OZKAN <boraozkan@gmail.com>"
31
            ],
32
            "ArgsEscaped": true,
33
            "Image": "sha256:e1af56d072b8d93fce4b566f4bf76311108dbbbe952b12a85418bd32c2fcdda7",
34
            "Volumes": null,
35
            "WorkingDir": "",
36
            "Entrypoint": null,
37
            "OnBuild": null,
38
            "Labels": {
39
                "maintainer": "Bora OZKAN <boraozkan@gmail.com>"
40
            }
41
        },
42
        "DockerVersion": "18.09.7",
43
        "Author": "",
44
        "Config": {
45
            "Hostname": "",
46
            "Domainname": "",
47
            "User": "",
48
            "AttachStdin": false,
49
            "AttachStdout": false,
50
            "AttachStderr": false,
51
            "Tty": false,
52
            "OpenStdin": false,
53
            "StdinOnce": false,
54
            "Env": [
55
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
56
            ],
57
            "Cmd": [
58
                "bash"
59
            ],
60
            "ArgsEscaped": true,
61
            "Image": "sha256:e1af56d072b8d93fce4b566f4bf76311108dbbbe952b12a85418bd32c2fcdda7",
62
            "Volumes": null,
63
            "WorkingDir": "",
64
            "Entrypoint": null,
65
            "OnBuild": null,
66
            "Labels": {
67
                "maintainer": "Bora OZKAN <boraozkan@gmail.com>"
68
            }
69
        },
70
        "Architecture": "amd64",
71
        "Os": "linux",
72
        "Size": 69208427,
73
        "VirtualSize": 69208427,
74
        "GraphDriver": {
75
            "Data": {
76
                "MergedDir": "/var/lib/docker/overlay2/495f15d329ebee16bec4985ea625d8d835be42adac19cf075a3b998535bbd58e/merged",
77
                "UpperDir": "/var/lib/docker/overlay2/495f15d329ebee16bec4985ea625d8d835be42adac19cf075a3b998535bbd58e/diff",
78
                "WorkDir": "/var/lib/docker/overlay2/495f15d329ebee16bec4985ea625d8d835be42adac19cf075a3b998535bbd58e/work"
79
            },
80
            "Name": "overlay2"
81
        },
82
        "RootFS": {
83
            "Type": "layers",
84
            "Layers": [
85
                "sha256:556c5fb0d91b726083a8ce42e2faaed99f11bc68d3f70e2c7bbce87e7e0b3e10"
86
            ]
87
        },
88
        "Metadata": {
89
            "LastTagTime": "2020-01-14T09:06:47.718230311Z"
90
        }
91
    }
92
]



4. Setting up Environment Variables

Shell
 




xxxxxxxxxx
1


 
1
#base image for container
2
FROM debian:buster-slim
3

          
4
LABEL maintainer='Bora Ozkan <boraozkan@gmail.com>'
5

          
6
ENV ASTERISK_VERSION 17-current
7
ENV OPUS_CODEC       asterisk-17.0/x86-64/codec_opus-17.0_current-x86_64


The ENV instruction sets the environment variable which is important while installing Asterisk. Above we have created two environment values; first is defining the version while the second is for using Default Codec.

Shell
 




xxxxxxxxxx
1
10


 
1
#base image for container
2
FROM debian:buster-slim
3

          
4
LABEL maintainer='Bora Ozkan <boraozkan@gmail.com>'
5

          
6
ENV ASTERISK_VERSION 17-current
7
ENV OPUS_CODEC       asterisk-17.0/x86-64/codec_opus-17.0_current-x86_64
8

          
9
COPY build-asterisk.sh /
10
RUN /build-asterisk.sh


The COPY instruction copies files to the image directory as specified while building image, while the RUN instruction will execute any commands in a new layer on top of the current image and commit results.

Now we can create our build-asterisk.sh

5. Declaring Commands

We start with declaring all commands to be run in the bash shell. For reference, we are checking if the ASTERISK_VERSIONis higher than 2.

Shell
 




xxxxxxxxxx
1
11


 
1
#!/bin/bash
2
PROGNAME=$(basename $0)
3

          
4
if test -z ${ASTERISK_VERSION}; then
5
  echo "${PROGNAME}: ASTERISK_VERSION required" >&2
6
  exit 1
7
fi
8

          
9
set -ex
10

          
11
useradd --system asterisk


The set -ex command is quite useful instructing the process to exit when the step fails in bash script, along with tracing errors within the bash script.

Using useradd command for creating new system account named 'Asterisk'.

Shell
 




xxxxxxxxxx
1
44


 
1
#!/bin/bash
2
PROGNAME=$(basename $0)
3

          
4
if test -z ${ASTERISK_VERSION}; then
5
  echo "${PROGNAME}: ASTERISK_VERSION required" >&2
6
  exit 1
7
fi
8

          
9
set -ex
10

          
11
useradd --system asterisk
12

          
13
apt-get update -qq
14
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
15
    autoconf \
16
    binutils-dev \
17
    build-essential \
18
    ca-certificates \
19
    curl \
20
    file \
21
    libcurl4-openssl-dev \
22
    libedit-dev \
23
    libgsm1-dev \
24
    libogg-dev \
25
    libpopt-dev \
26
    libresample1-dev \
27
    libspandsp-dev \
28
    libspeex-dev \
29
    libspeexdsp-dev \
30
    libsqlite3-dev \
31
    libsrtp2-dev \
32
    libssl-dev \
33
    libvorbis-dev \
34
    libxml2-dev \
35
    libxslt1-dev \
36
    procps \
37
    portaudio19-dev \
38
    unixodbc \
39
    unixodbc-bin \
40
    unixodbc-dev \
41
    odbcinst \
42
    uuid \
43
    uuid-dev \
44
    xmlstarlet


Above we are installing necessary libraries, as the base image being slim didn't had additional packages and modules.

It is very important in Docker that each step works smoothly without error. To ensure this we can DEBIAN_FRONTEND=noninteractive and parameters. This will essentially install all modules, packages and libraries in Quite Mode and not attempt to CLI. This will also automate our installation, reducing manual time and effort considerably.

Shell
 




xxxxxxxxxx
1
52


 
1
#!/bin/bash
2
PROGNAME=$(basename $0)
3

          
4
if test -z ${ASTERISK_VERSION}; then
5
  echo "${PROGNAME}: ASTERISK_VERSION required" >&2
6
  exit 1
7
fi
8

          
9
set -ex
10

          
11
useradd --system asterisk
12

          
13
apt-get update -qq
14
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
15
    autoconf \
16
    binutils-dev \
17
    build-essential \
18
    ca-certificates \
19
    curl \
20
    file \
21
    libcurl4-openssl-dev \
22
    libedit-dev \
23
    libgsm1-dev \
24
    libogg-dev \
25
    libpopt-dev \
26
    libresample1-dev \
27
    libspandsp-dev \
28
    libspeex-dev \
29
    libspeexdsp-dev \
30
    libsqlite3-dev \
31
    libsrtp2-dev \
32
    libssl-dev \
33
    libvorbis-dev \
34
    libxml2-dev \
35
    libxslt1-dev \
36
    procps \
37
    portaudio19-dev \
38
    unixodbc \
39
    unixodbc-bin \
40
    unixodbc-dev \
41
    odbcinst \
42
    uuid \
43
    uuid-dev \
44
    xmlstarlet
45
    
46
apt-get purge -y --auto-remove
47
rm -rf /var/lib/apt/lists/*
48

          
49
mkdir -p /usr/src/asterisk
50
cd /usr/src/asterisk
51

          
52
curl -vsL http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-${ASTERISK_VERSION}.tar.gz | tar --strip-components 1 -xz || \


apt-get purge is equivalent to apt-get remove --purge and will remove user data/configuration files. Above we are also creating folder in desired directory and install Asterisk source code from the Asterisk repository.

Shell
 




xxxxxxxxxx
1
60


 
1
#!/bin/bash
2
PROGNAME=$(basename $0)
3

          
4
if test -z ${ASTERISK_VERSION}; then
5
  echo "${PROGNAME}: ASTERISK_VERSION required" >&2
6
  exit 1
7
fi
8

          
9
set -ex
10

          
11
useradd --system asterisk
12

          
13
apt-get update -qq
14
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests \
15
    autoconf \
16
    binutils-dev \
17
    build-essential \
18
    ca-certificates \
19
    curl \
20
    file \
21
    libcurl4-openssl-dev \
22
    libedit-dev \
23
    libgsm1-dev \
24
    libogg-dev \
25
    libpopt-dev \
26
    libresample1-dev \
27
    libspandsp-dev \
28
    libspeex-dev \
29
    libspeexdsp-dev \
30
    libsqlite3-dev \
31
    libsrtp2-dev \
32
    libssl-dev \
33
    libvorbis-dev \
34
    libxml2-dev \
35
    libxslt1-dev \
36
    procps \
37
    portaudio19-dev \
38
    unixodbc \
39
    unixodbc-bin \
40
    unixodbc-dev \
41
    odbcinst \
42
    uuid \
43
    uuid-dev \
44
    xmlstarlet
45
    
46
apt-get purge -y --auto-remove
47
rm -rf /var/lib/apt/lists/*
48

          
49
mkdir -p /usr/src/asterisk
50
cd /usr/src/asterisk
51

          
52
curl -vsL http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-${ASTERISK_VERSION}.tar.gz | tar --strip-components 1 -xz || \
53

          
54
# 1.5 jobs per core works out okay
55
: ${JOBS:=$(( $(nproc) + $(nproc) / 2 ))}
56

          
57
./configure --with-resample \
58
            --with-pjproject-bundled \
59
            --with-jansson-bundled
60
make menuselect/menuselect menuselect-tree menuselect.makeopts


${JOBS:=$(( $(nproc) + $(nproc) / 2 ))} ensures performance and load balancing. Use ./configure and related parameters to make Asterisk ready for configuration.

6. Compiling Asterisk Modules -

The next step in the build process is to tell Asterisk which modules to compile and install, as well as set various compiler options. These settings are all controlled via a menu-driven system called Menuselect through interactive installation.


Options in Menuselect can be controlled from the command line. Menuselect can be built without invoking the user interface via the menuselect.makeopts as shown in the scipt below -

Shell
 




xxxxxxxxxx
1
26


 
1
make menuselect/menuselect menuselect-tree menuselect.makeopts
2

          
3
# disable BUILD_NATIVE to avoid platform issues
4
menuselect/menuselect --disable BUILD_NATIVE menuselect.makeopts
5

          
6
# enable required modules
7
menuselect/menuselect --enable BETTER_BACKTRACES menuselect.makeopts
8

          
9
# enable ooh323
10
menuselect/menuselect --enable chan_ooh323 menuselect.makeopts
11

          
12
# codecs
13
# menuselect/menuselect --enable codec_opus menuselect.makeopts
14
# menuselect/menuselect --enable codec_silk menuselect.makeopts
15

          
16
# # download more sounds
17
# for i in CORE-SOUNDS-EN MOH-OPSOUND EXTRA-SOUNDS-EN; do
18
#     for j in ULAW ALAW G722 GSM SLN16; do
19
#         menuselect/menuselect --enable $i-$j menuselect.makeopts
20
#     done
21
# done
22

          
23
# we don't need any sounds in docker, they will be mounted as volume
24
menuselect/menuselect --disable-category MENUSELECT_CORE_SOUNDS menuselect.makeopts
25
menuselect/menuselect --disable-category MENUSELECT_MOH menuselect.makeopts
26
menuselect/menuselect --disable-category MENUSELECT_EXTRA_SOUNDS menuselect.makeopts


As shown above, some modules have been enabled/disabled as required using menuselect.makeopts.

Shell
 




xxxxxxxxxx
1
28


 
1
make -j ${JOBS} all
2
make install
3

          
4
# copy default configs
5
# cp /usr/src/asterisk/configs/basic-pbx/*.conf /etc/asterisk/
6
make samples
7

          
8
# set runuser and rungroup
9
sed -i -E 's/^;(run)(user|group)/\1\2/' /etc/asterisk/asterisk.conf
10

          
11
# Install opus
12
mkdir -p /usr/src/codecs/opus \
13
  && cd /usr/src/codecs/opus \
14
  && curl -vsL http://downloads.digium.com/pub/telephony/codec_opus/${OPUS_CODEC}.tar.gz | tar --strip-components 1 -xz \
15
  && cp *.so /usr/lib/asterisk/modules/ \
16
  && cp codec_opus_config-en_US.xml /var/lib/asterisk/documentation/
17

          
18
mkdir -p /etc/asterisk/ \
19
         /var/spool/asterisk/fax
20

          
21
chown -R asterisk:asterisk /etc/asterisk \
22
                           /var/*/asterisk \
23
                           /usr/*/asterisk
24
chmod -R 750 /var/spool/asterisk
25

          
26
cd /
27
rm -rf /usr/src/asterisk \
28
       /usr/src/codecs


With make install we are finishing menuselect and the build of our Asterisk image. Additionally the sed command will modify asterisk.conf and -E add the script to be executed.

Shell
 




xxxxxxxxxx
1
18


 
1
# remove *-dev packages
2
devpackages=`dpkg -l|grep '\-dev'|awk '{print $2}'|xargs`
3
DEBIAN_FRONTEND=noninteractive apt-get --yes purge \
4
  autoconf \
5
  build-essential \
6
  bzip2 \
7
  cpp \
8
  m4 \
9
  make \
10
  patch \
11
  perl \
12
  perl-modules \
13
  pkg-config \
14
  xz-utils \
15
  ${devpackages}
16
rm -rf /var/lib/apt/lists/*
17

          
18
exec rm -f /build-asterisk.sh


Finally clean the Dev packages(if any) from the Docker image. That's it!

Let us go back to the Dockerfile and continue creating our image.

Shell
 




xxxxxxxxxx
1
17


 
1
#base image for container
2
FROM debian:buster-slim
3

          
4
LABEL maintainer='Bora Ozkan <boraozkan@gmail.com>'
5

          
6
ENV ASTERISK_VERSION 17-current
7
ENV OPUS_CODEC       asterisk-17.0/x86-64/codec_opus-17.0_current-x86_64
8

          
9
COPY build-asterisk.sh /
10
RUN /build-asterisk.sh
11

          
12
EXPOSE 5060/udp 5060/tcp
13
VOLUME /var/lib/asterisk/sounds /var/lib/asterisk/keys\                       /var/lib/asterisk/phoneprov /var/spool/asterisk\                       /var/log/asterisk
14

          
15
COPY docker-entrypoint.sh /
16
ENTRYPOINT ["/docker-entrypoint.sh"]
17
CMD ["/usr/sbin/asterisk", "-vvvdddf", "-T", "-W", "-U", "asterisk", "-p"]


EXPOSE informs Docker that the container listens on the specified network ports during runtime. You can specify whether the port listens on TCP or UDP, where the default is TCP if the protocol is not specified.
Use the VOLUME instruction to create a mount point with the specified name and mark it as holding externally mounted volumes from native host or other containers. We can  also add custom codec, sound and log from our local directory to the image.

Additionally, ENTRYPOINT allows you to configure a container that will run as an executable and will start Asterisk when Docker run is executed. Let us put some additional commands in docker-entrypoint.sh -

Shell
 




xxxxxxxxxx
1
24


 
1
#!/bin/sh
2

          
3
# run as user asterisk by default
4
ASTERISK_USER=${ASTERISK_USER:-asterisk}
5

          
6
if [ "$1" = "" ]; then
7
  COMMAND="/usr/sbin/asterisk -T -W -U ${ASTERISK_USER} -p -vvvdddf"
8
else
9
  COMMAND="$@"
10
fi
11

          
12
if [ "${ASTERISK_UID}" != "" ] && [ "${ASTERISK_GID}" != "" ]; then
13
  # recreate user and group for asterisk
14
  # if they've sent as env variables (i.e. to macth with host user to fix permissions for mounted folders
15

          
16
  deluser asterisk && \
17
  adduser --gecos "" --no-create-home --uid ${ASTERISK_UID} --disabled-password ${ASTERISK_USER} || exit
18

          
19
  chown -R ${ASTERISK_UID}:${ASTERISK_UID} /etc/asterisk \
20
                                           /var/*/asterisk \
21
                                           /usr/*/asterisk
22
fi
23

          
24
exec ${COMMAND}


The above script will create an Asterisk user for us, also checking if user is working with correct permissions. With this we finish our Dockerfile script which will help installing and configuring Asterisk. Now it is time to build our image from the Dockerfile.

docker build -t asterisk:latest .

With this command you are creating image from the Dockerfile.

Shell
 




xxxxxxxxxx
1


 
1
#docker images
2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
3
asterisk            latest              e275d4a274de        4 minutes ago       421MB


As shown above, Asterisk image created is now of 421Mb as we added certain modules and installation scripts.

7. Testing the Asterisk Image

Let us test the Asterisk image by starting our container with the Expose Port parameter. Run your container with the command below and you should see an output similar to this:

Shell
 




xxxxxxxxxx
1


 
1
docker run -p 5060:5060 asterisk:latest



The port 5060 should be reachable now which you can test further by registering with a softphone. It is however important that you expose a range of ports when running the container using -P (capital P). Normally when you start a container it is reachable from container to internet but cannot reach from outside to container. In this case, if you use -P with docker run, the port will be exposed once for TCP and once for UDP. Remember that -P uses an ephemeral high-ordered host port on the host, so the port will not be the same for TCP and UDP.

docker run -P asterisk:latest

Now we are ready to push our images to our Docker Hub account; which we will use for Fargate and ECR in AWS Cloud.

Shell
 




xxxxxxxxxx
1


 
1
docker login --username=yourhubusername --email=youremail@company.com


After succesfully login to the docker hub account we are ready to push our images to the hub -

Shell
 




xxxxxxxxxx
1


 
1
docker tag e275d4a274de boraozkan/asterisk_fargate:firsttry


It is also advised to tag your images to make it easier to remember as above. Following which you can push it to the Docker -

Shell
 




xxxxxxxxxx
1


 
1
#docker push boraozkan/asterisk_fargate
2
The push refers to repository [docker.io/boraozkan/asterisk_fargate]81fa04b8a12f: Pushed24243a5e2b77: Pushing  45.21MB/352.1MB23d7848c4bd6: Pushed556c5fb0d91b: Mounted from library/debian




Deploying Asterisk Docker Image on Fargate through ECR

1. Setting up AWS EC2 Image for Asterisk

a. Let us start by creating an EC2 instance which will host our Asterisk image and registering it to the ECR.

b. Open AWS console and launch a new instance on EC2 -

c. Select Amazon Linux 2 AMI and choose free-tier instance t2.nano or t2.micro (as per your choice). Follow instructions and launch machine. Keep your PEM file which we will need while login to SSH. Navigate to EC2 console again and wait a couple of minutes until the new instance gets ready. Click new instance when it is available, following which copy the IP (ours is 35.153.102.33 as shown below) -

d. After creating the instance, assign IAM Role to the instance which essentially gives permission to access ECR(Elastic Container Registry) service. For doing the above, you can go to Action Menu > Instance Settings > Attach/Replace IAM Role

e. Choose EC2 >> Go to next page >> Search for 'elastic container service' >> Apply for this permission >> Assign a name tag for ease of remembrance.

f. Choose EC2 IAM role from the console >> Click on Apply.

g. Login instance through SSH.

Shell
 




xxxxxxxxxx
1


 
1
ssh -i xxx.pem ec2-user@35.153.102.33
2

          
3
The authenticity of host '35.153.102.33 (35.153.102.33)' can't be established.ECDSA key fingerprint is SHA256:PAQBN8RnB8YeP/eOG/VoJEmn4ipTpZAoZ/0Lb9rgOx8.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '35.153.102.33' (ECDSA) to the list of known hosts.
4

          
5
https://aws.amazon.com/amazon-linux-2/2 package(s) needed for security, out of 13 availableRun "sudo yum update" to apply all updates.[ec2-user@ip-172-31-39-17 ~]$


Change your PEM file name as per your convenience >> Assign permission with chmod 600 or 700 >> Login to a fresh instance >> Make necessary updates on instance as required.

[ec2-user@ip-172-31-39-17 ~]$ sudo yum -y update

[ec2-user@ip-172-31-39-17 ~]$ sudo amazon-linux-extras install docker

[ec2-user@ip-172-31-39-17 ~]$ sudo service docker start

[ec2-user@ip-172-31-39-17 ~]$ sudo usermod -a -G docker ec2-user

[ec2-user@ip-172-31-39-17 ~]$ sudo docker info

sudo docker login

Enter your credentials and then extract the Asterisk image which we created on Docker hub.

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ sudo docker pull boraozkan/asterisk_fargate:firsttry
2
firsttry: Pulling from boraozkan/asterisk_fargate
3
8ec398bc0356: Pull complete 
4
cfb6ad1933f7: Pull complete 
5
3ac5bd4b41b7: Pull complete 
6
985634a4d9b2: Pull complete 
7
Digest: sha256:f519a1f04018f34f0e17380492a0722adf360c9e2f021a951d7acaa3a7533c4d
8
Status: Downloaded newer image for boraozkan/asterisk_fargate:firsttry
9
[ec2-user@ip-172-31-39-17 ~]$ 


The Asterisk image is now on our EC2 instance -  

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ sudo docker images
2
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
3
boraozkan/asterisk_fargate   firsttry            e275d4a274de        19 hours ago        421MB
4
[ec2-user@ip-172-31-39-17 ~]$ 


The next step is to push this image to ECR, however before pushing it to ECR we need to first create a repository in ECR.

2. Creating ECR Repository on AWS EC2 -

a. Go to the ECR console and create a repository.


b. Copy the URI after create the repository


c. Tag your image with this URI in EC2 SSH CLI. Note that we now have two images; one the originally configured Asterisk image while the second one is the one which we tagged in ECR.

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ sudo docker tag boraozkan/asterisk_fargate:firsttry 312867154612.dkr.ecr.us-east-1.amazonaws.com/asterisk
2
[ec2-user@ip-172-31-39-17 ~]$ sudo docker images
3
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
4
312867154612.dkr.ecr.us-east-1.amazonaws.com/asterisk   latest              e275d4a274de        19 hours ago        421MB
5
boraozkan/asterisk_fargate                              firsttry            e275d4a274de        19 hours ago        421MB
6
[ec2-user@ip-172-31-39-17 ~]$ 


d. Login to ECR from EC2 AWS CLI >> Keep note of the long key.

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ aws ecr get-login --no-include-email --region us-east-1
2
docker login -u AWS -p eyJwYXlsb2FkIjoiVFd5UVc4UXhmZ1R0WnFNY1FZd25ITVVTcVZTOFlvZHo2L0l6SDNQMGpCUHpSOFJ1c3JvSjRNems1OFI1ak5kY2QwQldnN


e. Copy the portion - docker login -u AWS -p eyJwYXlsb2FkIjoiVFd5UVc4UXhmZ1R0WnFNY1FZd25ITVVTcVZTOFlvZHo2L0l6SDNQMGpCUHpSOFJ1c3JvSjRNems1OFI1ak5kY2QwQldnN and paste to SSH CLI to login to ECR

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ sudo docker login -u AWS -p eyJwYXlsb2FkIjoiVFd5UVc4UXhmZ1R0WnFNY1FZd25ITVVTcVZTOFlvZHo2L0l6SDNQMGpCSFcwREVNZkxHL2hpOElIc000UGdpTUVhS2hLWT0iLCJ2ZXJzaW9uIjoiMiIsInR5cGUiOiJEQVRBX0tFWSIsImV4cGlyYXRpb24iOjE1NzkxMjc2MDN9 https://312867154612.dkr.ecr.us-east-1.amazonaws.com
2
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
3
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
4
Configure a credential helper to remove this warning. See
5
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
6

          
7
Login Succeeded
8
[ec2-user@ip-172-31-39-17 ~]$


f. We are ready to push our image to ECR Repo now. Use URI which was copied during the creation of the ECR repository.

Shell
 




xxxxxxxxxx
1


 
1
[ec2-user@ip-172-31-39-17 ~]$ sudo docker push 312867154612.dkr.ecr.us-east-1.amazonaws.com/asterisk
2
The push refers to repository [312867154612.dkr.ecr.us-east-1.amazonaws.com/asterisk]
3
81fa04b8a12f: Pushed 
4
24243a5e2b77: Pushed 
5
23d7848c4bd6: Pushed 
6
556c5fb0d91b: Pushed 
7
latest: digest: sha256:f519a1f04018f34f0e17380492a0722adf360c9e2f021a951d7acaa3a7533c4d size: 1157
8
[ec2-user@ip-172-31-39-17 ~]$ 


g. Check ECR Repo for the Asterisk image. Once we see it there, we are can now deploy the image to Fargate.

3. Deploying Asterisk Image to Fargate

a. On the AWS main console's ECS page click on 'Get Started' and you will be in the screen as below -

b. Start configuring by giving a name to the container >> Assign a Task >> Select 'Custom' container definition >> Type the Image URI Container name and expose ports.

c. Assign Default for every other options.  For the reference of this article, we are not assigning any Load Balancer in this project

d. Leave Default Cluster Configuration -

e. Check reviews and create Fargate. Once finished, go back to the Service View

f. Use a softphone and test by trying to register.

Conclusion

Hopefully you can now understand and use your Asterisk image to deploy your PBX with Fargate. In another topic we will go through the steps of arranging your Asterisk RTP for voice transfer.


This article was originally published on https://appfleet.com/blog/serverless-asterisk-with-docker-and-aws-fargate/ and has been authorized by Appfleet for a republish.

AWS Docker (software) Asterisk (PBX) shell

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Container Checkpointing in Kubernetes With a Custom API
  • Leveraging Seekable OCI: AWS Fargate for Containerized Microservices
  • Deploying Dockerized Applications on AWS Lambda: A Step-by-Step Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!