Deploying Go applications on cloud platforms such as AWS, GCP, and Azure involves understanding the specific services and deployment strategies each platform offers. This section provides a detailed guide on how to deploy Go applications using the most commonly used services from each cloud provider.
Using AWS Elastic Beanstalk
Overview: Elastic Beanstalk is a Platform as a Service (PaaS) that simplifies the process of deploying, managing, and scaling web applications and services.
Steps to Deploy:
sheb init -p go go-app --region us-west-2
sheb create go-app-env
sheb deploy
sheb open
Using AWS Lambda with API Gateway
Overview: AWS Lambda allows you to run code without provisioning or managing servers. API Gateway provides a way to create and manage APIs.
Steps to Deploy:
gopackage main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(ctx context.Context, name string) (string, error) {
return fmt.Sprintf("Hello, %s!", name), nil
}
func main() {
lambda.Start(handler)
}
shGOOS=linux GOARCH=amd64 go build -o main main.go zip deployment.zip main
shaws lambda create-function --function-name go-lambda --zip-file fileb://deployment.zip --handler main --runtime go1.x --role arn:aws:iam::account-id:role/execution_role
Using Kubernetes on AWS (EKS)
Overview: Amazon Elastic Kubernetes Service (EKS) is a managed Kubernetes service.
Steps to Deploy:
sheksctl create cluster --name go-app-cluster --region us-west-2
yaml# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 2
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: your-docker-image
ports:
- containerPort: 8080
yaml# service.yaml
apiVersion: v1
kind: Service
metadata:
name: go-app
spec:
type: LoadBalancer
selector:
app: go-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
shkubectl apply -f deployment.yaml kubectl apply -f service.yaml
Using Google App Engine
Overview: Google App Engine is a fully managed serverless platform that automatically scales your application.
Steps to Deploy:
yaml# app.yaml
runtime: go116
handlers:
- url: /.*
script: auto
shgcloud app deploy
Using Google Kubernetes Engine (GKE)
Overview: Google Kubernetes Engine is a managed Kubernetes service that lets you run containerized applications.
Steps to Deploy:
shgcloud container clusters create go-app-cluster --num-nodes=3
shkubectl apply -f deployment.yaml kubectl apply -f service.yaml
Using Cloud Run
Overview: Cloud Run is a managed compute platform that automatically scales stateless containers.
Steps to Deploy:
shgcloud builds submit --tag gcr.io/PROJECT-ID/go-app
shgcloud run deploy go-app --image gcr.io/PROJECT-ID/go-app --platform managed
Using Azure App Service
Overview: Azure App Service is a fully managed platform for building, deploying, and scaling web apps.
Steps to Deploy:
shaz webapp create --resource-group myResourceGroup --plan myAppServicePlan --name go-app --runtime "GO|1.14"
shaz webapp up --name go-app --runtime "GO|1.14"
Using Azure Kubernetes Service (AKS)
Overview: Azure Kubernetes Service is a managed Kubernetes service.
Steps to Deploy:
shaz aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys
shkubectl apply -f deployment.yaml kubectl apply -f service.yaml
Using Azure Functions
Overview: Azure Functions is a serverless compute service that lets you run event-driven code.
Steps to Deploy:
shfunc init MyFunctionApp --docker
func new --language go --template "HTTP trigger" --name MyFunction
shfunc azure functionapp publish myFunctionApp
CI/CD with GitHub Actions:
yamlname: Deploy to AWS
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Build
run: go build -o main .
- name: Deploy to AWS
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2
run: |
zip deployment.zip main
aws lambda update-function-code --function-name go-lambda --zip-file fileb://deployment.zip
CI/CD with Google Cloud Build:
yamlsteps:
- name: 'gcr.io/cloud-builders/go'
args: ['build', '-o', 'main', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/go-app', '.']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'go-app', '--image', 'gcr.io/$PROJECT_ID/go-app', '--platform', 'managed']
CI/CD with Azure Pipelines:
yamltrigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseGoTool@0
inputs:
version: '1.16'
- script: |
go build -o main .
displayName: 'Build Go application'
- task: AzureCLI@2
inputs:
azureSubscription: 'your-subscription-id'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
displayName: 'Deploy to AKS'
By following these guidelines, you can effectively deploy Go applications on AWS, GCP, and Azure, leveraging their respective services to ensure scalability, reliability, and efficient resource utilization.