AWS

Getting started with Opta on AWS.

To use Opta, you first need to create some simple yaml configuration files that describe your needs. In the following guide we will deploy a simple python flask application.

1: Installation

For Opta to work, the prerequisite tools needed are:

Then you can simply install opta using this command (detailed instructions):

/bin/bash -c "$(curl -fsSL https://docs.opta.dev/install.sh)"

Make sure the AWS cloud credentials are configured in your terminal for the account you would be using in this demo.

2: Environment creation

Before you can deploy your app, you need to first create an environment (like staging, prod etc.) This sets up the base infrastructure (like network and cluster) that is the foundation for your app.

Note that it costs around $5 per day to run this on AWS. So make sure to destroy it after you’re done (opta has a destroy command so it should be easy :))!

Create this file and name it opta.yaml

# opta.yaml
name: staging # name of the environment
org_name: my-org # A unique identifier for your organization
providers:
  aws:
    region: us-east-1
    account_id: XXXX # Your 12 digit AWS account id
modules:
  - type: base
  - type: k8s-cluster
  - type: k8s-base

Now, run:

opta apply

For the first run, this step takes approximately 15 min.
It configures 3 Opta modules:

For more information about what is created, see AWS Architecture.

Once done, the apply command lists all the resource created, for example:

# partial output of opta apply

k8s_cluster_name = [The Kubernetes cluster name]
load_balancer_raw_dns = [Load Balancer hostname]

Opta updates complete!

3: Service creation

In this step we will create a service - which is basically a http server packaged in a docker container.
Here is a simple hello world app, the source code is here.

Create a new opta file for your service.

# hello.yaml
name: hello
environments:
  - name: staging
    path: "opta.yaml" # the file we created in previous step
modules:
  - type: k8s-service
    name: hello
    port:
      http: 80
    # from https://github.com/run-x/hello-opta
    image: ghcr.io/run-x/hello-opta/hello-opta:main
    healthcheck_path: "/"
    # path on the load balancer to access this service
    public_uri: "/hello"

Now you are ready to deploy your service.

opta apply -c hello.yaml
# partial output of opta apply -c hello.yaml
hello-hello-k8s-service-586447679-fgmld  * Running on http://10.0.147.114:80/
...
module.hello.helm_release.k8s-service: Creation complete after 53s [id=staging-hello]

Opta updates complete!

Now, your service is deployed, you can:

  • Access your service using the load balancer (public)
# see output above or run `opta output | grep load_balancer_raw_dns`
export load_balancer_raw_dns=...

# the service is reachable at /hello (set in the `public_uri` property)
curl http://$load_balancer_raw_dns/hello

<p>Hello from Opta.!</p>
  • SSH into the container
opta shell -c hello.yaml

root@staging-hello-k8s-service-57d8b6f478-vwzkc:/#
  • See the application logs
opta logs -c hello.yaml             

Showing the logs for server hello-hello-k8s-service-586447679-fgmld of your service
hello-hello-k8s-service-586447679-fgmld  * Running on http://10.0.147.114:80/
hello-hello-k8s-service-586447679-fgmld 127.0.0.1 - - [23/Dec/2021 19:42:18] "GET / HTTP/1.1" 200 -
  • If you have kubectl installed, you can use it to connect to the kubernetes cluster
# configure the kubeconfig file
# note: if you don't want to use the default path $HOME/.kube/config, set the env var KUBECONFIG first
opta configure-kubectl

# Opta created all the kubernetes resources for your service
kubectl get all --namespace hello

NAME                                            READY   STATUS    RESTARTS   AGE
pod/hello-hello-k8s-service-586447679-fgmld   2/2     Running   0          17m

NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/hello   ClusterIP   172.20.221.139   <none>        80/TCP    17m

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-hello-k8s-service   1/1     1            1           17m

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-hello-k8s-service-586447679   1         1         1       17m

NAME                                                            REFERENCE                              TARGETS           MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hello-hello-k8s-service   Deployment/hello-hello-k8s-service   18%/80%, 1%/80%   1         3         1          17m

4: Cleanup

Once you’re finished playing around with these examples, you may clean up by running the following command from the environment directory:

# destroy the service resources
opta destroy -c hello.yaml

# destroy the environment resources
opta destroy -c opta.yaml

5: Next steps


Last modified August 5, 2022 : Cleanup install script (#197) (2175394)