Here, we have an EventBridge rule watching for tagging operations against S3 objects in our bucket. When detected, our Lambda is invoked which, loads each record of the CSV as an item in a DynamoDB table.
An Amazon CloudWatch alarm which, is monitoring the ScanBacklogPerTask metric, notifies the Application Auto Scaling service.
Application Auto Scaling updates the running task count of an ECS service.
The tasks in the ECS service mount the EFS file system so that the latest ClamAV virus definitions are available.
The tasks then receive messages from the SQS queue.
Each message contains details of the S3 object to be scanned. The task downloads the object and performs a clamdscan on it.
The result of the virus scan (either “CLEAN” or “INFECTED”) is set as the “av-status” tag on the S3 object.
Note also that the ECS scan service runs in a protected VPC subnet. That is, a subnet which has no internet access.
The Docker code for the ECS tasks can be found at aw5academy/docker/clamav. The Docker containers built from this code poll SQS for messages and perform the ClamAV virus scan. We will come back to this later.
One last step is we need to trigger a run of the freshclam task so that the ClamAV database files are present on our EFS file system. The easiest way to do this is to update the schedule for the task from the ECS console and set it to run every minute.
We can verify that the database is updated from the task logs.
Now let’s test our solution by uploading a file directly to the S3 bucket. When we do, we can check the metrics for our SQS queue for activity as well as the logs for the ECS scan tasks.
Success! We can see from the metrics that a message was sent to the queue and deleted shortly after. And the ECS logs show the file being scanned and the S3 object being tagged.
As one final test, let’s see if a virus will be detected and appropriate action taken. This solution has been designed to block access to all objects uploaded to S3 unless they have been tagged with “av-status-CLEAN”. So we expect to have no access to a virus infected file.
Rather than using a real virus we will use the EICAR test file. Let’s upload a file with this content to see what happens.
Great! The object has been properly tagged as infected. But are we blocked from accessing the file? Let’s try downloading it.
We are denied as expected.
Now let’s check out part 3 where we implement the loading of our CSV data.
Suppose a file transfer workload exists between a business and their customers. A comma-separated values (CSV) file is transferred to the business and the records are loaded into a database. The business has regulatory requirements mandating that all external assets are virus scanned before being processed. Additionally, an intrusion prevention system (IPS) must operate on all public endpoints.
In the following 3 articles I will demonstrate how we can build a serverless system that meets these requirements.
Make a note of both the bucket-name and sftp-endpoint outputs… we will use both of these values later.
With Terraform applied we can inspect the created components in the AWS console. Let’s first check our SFTP endpoint which can be found in the AWS Transfer Family service.
We can also see the AWS Network Firewall which is in the VPC service.
Let’s test out our solution. First, in the root of the Terraform directory, an example.pem file exists which is the private key we will use to authenticate with the SFTP endpoint. Copy this to your Windows host machine so we can use it with WinSCP.
In WinSCP, create a new site and provide the sftp endpoint. For username we will use “example”.
Select “Advanced” and provide the path to the example.pem you copied over. It will require you to convert it to a ppk file.
Now login and copy a file across.
Lastly, verify the file exists in S3 from the AWS console.
Now let’s continue with part 2 where we will implement the anti-virus scanning.
This article will be a little bit different to previous posts. Having only just recently started to check out AWS Machine Learning I am still in the early stages of my study of these services. So for this article, I wanted to post what I have learned so far in the form of a possible usage for machine learning — automated UI testing.
Let’s suppose we have a web application that provides a listing of search results — maybe a search engine or some kind of eCommerce website. We want to ensure the listings are displaying correctly so we have humans perform UI testing. Can we train machines to do this work for us?
The most difficult part of building a machine learning model appears to be collecting the right training data. Our training data will consist of screenshots of the web page where the “good” images will be when the application is working as expected and the “bad” images are when there is some error in the display of the application.
We can then explode our test data by performing random orientation changes, contrast changes etc. This increases the number of images in our training set.
Now we can open the Amazon SageMaker service and create our training job. We upload the training data to an Amazon S3 bucket so that SageMaker can download it.
Once created, the training job will start. We can view metrics from the job as it is working.
You can see the training accuracy improving over time.
Now that we have our model trained, we can test how good it is by deploying it to a SageMaker Model Endpoint. Once deployed, we can test it with invoke-endpoint. We provide a screenshot image to this API call and the result returned to us will be two values: the probability of the image being “good” and the probability of it being “bad”.
A partial success! The model did well for some tests and not so well for others.
Some thoughts and conclusions I have made after completing this experiment:
The algorithm used in this model was Image Classification. I am not sure this is the best choice. Most of the “good” images are very similar. Probably too similar. We might need another algorithm which, rather than classify the image, detects abnormalities.
As mentioned earlier, gathering the training data is the difficult part. It is possible that this mock application is not capable of producing enough variation. A real world application may produce better results. Additionally, actual errors observed in the past could be used to train the model.
Even with the less than great results from this experiment, this solution could be used in a CI/CD pipeline. The sample errors I generated were sometimes very subtle, such as text being off by a few pixels. The model could be retrained to detect only very obvious errors. Then, an application’s build pipeline could do very quick sanity tests to detect obvious UI errors.
A recent AWS Fargate feature update has added support for S3 hosted environment files. In this article I will show how you could use this to manage your application’s configuration. I will also demonstrate how changes to the configuration can be released in a blue-green deployment.
The solution we will build will follow the design shown in the below diagram.
If you have any issues with this step, navigate to the CodeCommit service and open the ecs-env-file-demo repository for clone instructions and prerequisites.
As soon as we push our code to CodeCommit, our release pipeline will trigger. Navigate to the CodePipeline service and open the ecs-env-file-demo pipeline.
Wait until this release completes.
Application Configuration Changes
We can now test our process for making configuration changes. Navigate to the CodeCommit service and open our ecs-env-file-demo repository. Then open the cfg.env file. You can see that our configuration file has a value of “blue” for our CSS_BACKGROUND variable. This is the variable that our Apache server uses for the webpage’s background colour.
Let’s change this value to “green”, enter the appropriate Author details and click “Commit changes”.
We can now use the CodeDeploy service to follow our deployment. If you first navigate to the CodePipeline service and open our ecs-env-file-demo pipeline, when the CodeDeploy stage begins, click on the Details link to bring us to the CodeDeploy service.
Our deployment has started. Note, our deployments will use a Canary release with 20% of the traffic receiving the new changes for 5 minutes. After that, 100% of the traffic will receive the new changes. In your checkout of the Terraform code, there is a deployment-tester.html file. This is a page of 9 HTML iframes with the source being the DNS name of the load balancer in our application stack. The page auto refreshes every 5 seconds.
If you open this deployment-tester.html file (you may need to open developer tools and disable cache for it to be effective) you will be able to verify our release is working as expected. It should initially show just the original blue.
Now you can wait for CodeDeploy to enter the next stage.
We now have 20% of our traffic routed to the new application configuration — the green. Let’s check this in our deployment-tester.html file:
And to complete the process, we can wait for CodeDeploy to finish and verify the application is fully green.
Cleanup the created resources with:
I hope this very simple example has effectively demonstrated the new capability in AWS Fargate.
In this article I will show how you can run your AWS CodeBuild projects locally. AWS CodeBuild is a “fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy”. By running your CodeBuild projects locally you can test code changes before committing, allowing you to rapidly develop and debug your projects.
We will use the EC2 instance as a mock for an application that needs to communicate with our Aurora database.
Note: at the time of writing this article, Terraform does not support RDS Proxy resources. So we will need to manually create this component from the AWS console.
Let’s first deploy our Terraform code with:
git clone https://gitlab.com/aw5academy/terraform/rds-proxy.git
Once Terraform has been applied, it is worth examining the security groups that were created.
We can see that the Aurora database only allows connections from the Proxy and the Proxy only allows connections from the EC2 instance.
Additionally, a Secrets Manager secret was created. Our RDS Proxy will use the values from this secret to connect to our database. Note how it is the proxy alone that uses these credentials. We will see later that our application (the EC2 instance) will use IAM authentication to establish a connection with the RDS proxy and so the application never needs to know the database credentials.
Now we can create our RDS proxy from the AWS RDS console. During the creation of the proxy, provide the following settings
Select PostgreSQL for Engine compatibility;
Tick Require Transport Layer Security;
Select rds-proxy-test for Database;
Select the secret with prefix rds-proxy-test for Secrets Manager secret(s);
Select rds-proxy-test-proxy-role for IAM role;
Select Required for IAM authentication;
Select rds-proxy-test-proxy for Existing VPC security groups;
Now wait for the proxy to be created. This can take some time. Once complete, obtain the RDS Proxy endpoint from the console which, we will use to connect to from our EC2 instance.
Let’s test our setup. SSH into the EC2 instance with:
The RDS Proxy feature can improve application security as we have seen, with the proxy alone having access to the database credentials and the application using IAM authentication to connect to the proxy.
Application resilience is improved since RDS Proxy improves failover times by up to 66%.
Lastly, your applications will be able to scale more effectively since RDS Proxy will pool and share connections to the database.
To cleanup the resources we created, first delete the RDS Proxy from the console and then from your terminal, destroy the Terraform stack with:
In this article I will show how you can launch an Amazon Linux EC2 instance with a desktop environment that will serve as a jumpbox. Connections to this jumpbox will be made through RDP via a session manager port tunneling session. By using session manager, our EC2 instance’s security group does not require ingress rules allowing RDP or other ports to connect, thus improving the security of the jumpbox.
Before continuing with this article I would strongly recommend reading my earlier article Access Private EC2 Instances With AWS Systems Manager Session Manager. That article will explain the fundamental workings of session manager and shows how to deploy resources to your AWS account that will be required for setting up the jumpbox described in this article.
When the session-manager stack is deployed we need to read some of the Terraform outputs as we will need their values for the jumpbox stack’s input variables. We can retrieve the outputs and set them as environment variables with:
git clone https://gitlab.com/aw5academy/terraform/jumpbox.git
After the stack deploys, wait approximately 5 minutes. This is to allow time for the converge of the aw5academy/chef/jumpbox Chef cookbook which, is part of the EC2 instance’s user data. This cookbook installs the MATE desktop environment on the Amazon Linux instance. Also see here for more information on installing a GUI on Amazon Linux.
Let’s make sure we can connect to the jumpbox with a terminal session. The jump.sh script can be used:
You should see something like the following:
Now we can try a remote desktop session. Terminate the terminal session with exit and then run:
bash jump.sh -d
You should now see the port forwarding session being started:
Also printed are the connection details for RDP. Open your RDP client and enter localhost:55678 for the computer to connect to and provide the supplied user name. Check the Allow me to save credentials option and click Connect:
Provide the password at the prompt and click OK:
Behind The Scenes
An explanation of what is occurring when we use our jump.sh script…
In order to start an RDP session the client needs to know the username and password for an account on the jumpbox. Rather than creating a generic account to be shared among clients we dynamically create temporary (1 day lifetime) accounts. This is accomplished through the following actions:
The client creates a random username using urandom;
The client creates a random password using urandom;
The client creates a SHA-512 hash of the password using openssl;
The jumpbox retrieves the hashed password from parameter store;
The jumpbox deletes the hashed password from parameter store;
The jumpbox creates an account with the provided username and the retrieved hash of the password;
The jumpbox marks the account and password to expire after 1 day;
With these steps, the password never leaves the client and is always stored either encrypted and/or hashed and is only stored for as long as it is required.
That’s all there is to it. After your jumpbox is enabled you can configure your private applications to accept traffic from the jumpbox’s security group. The chromium browser can then be used to access these applications securely. I hope you find this article useful.
Session Manager is a fully managed AWS Systems Manager capability that lets you manage your EC2 instances, on-premises instances, and virtual machines (VMs) through an interactive one-click browser-based shell or through the AWS CLI. Session Manager provides secure and auditable instance management without the need to open inbound ports, maintain bastion hosts, or manage SSH keys.
Let’s try it out. First, we will use the AWS CLI to launch a new EC2 instance in the private subnet that was created by the Terraform code. This instance will have no key pair and will use the VPC’s default security group which allows no inbound traffic from outside the VPC.
That’s it! You are now connected to a private EC2 instance in your VPC which, has no public IP, no key pair and no inbound access from outside the VPC defined in its security group.
As well as starting a shell session on an instance you can also use session manager to start a port forwarding session. Suppose you have an EC2 instance with a tomcat server running on port 8080, you could start a port forwarding session that maps local port 18080 to the instance’s port of 8080:
By default, session manager sessions are launched via a system-generated ssm-user. We can change this by launching the session manager preferences, checking the `Enable Run As support for Linux instances option and providing the alternative user.
Now when we start a session we are logged in as this user:
Additionally, you may add a tag to IAM roles or users with the tag key being SSMSessionRunAs and the tag value being the user account to login with. This allows you to further control access to your EC2 instances. See here for more details on this.
I hope this article demonstrates both how useful session manager is and how easy it is to setup and configure. Beyond the advantages described above you also get a full log of all sessions delivered to a CloudWatch log group and an S3 bucket for auditing purposes. These are configured in the Terraform code I have provided.
AWS has recently announced support for Amazon Elastic File System (EFS) within AWS Lambda. This change creates new possibilities for serverless applications. In this article I will demonstrate one such possibility — centralising the storage and updating of the ClamAV virus database.
ClamAV® is an open source antivirus engine for detecting trojans, viruses, malware & other malicious threats.
Like any antivirus solution, ClamAV needs to be kept up to date to be fully effective. Ordinarily the virus database can be updated by issuing the freshclam command. However, this requires that the instance running the command have internet access. When developing secure architectures in public cloud it is sometimes necessary to have fully isolated subnets which, do not have internet access. Additionally, strict security compliance requirements may dictate that virus definitions are not updated directly from the internet but instead be updated from a centralised location within the VPC.
Combining EFS, Lambda and EC2 we can create a configuration that will meet these requirements.
The below diagram represents the architecture we will implement.
Our virus database will be stored on an EFS file system. EC2 instances will be configured to use this file system for their virus definitions (we will deploy the instance in a public subnet in this example just to keep things simple). A “freshclam” Lambda function will keep the virus database stored on EFS up to date.
Deploy the stack by issuing the following commands:
git clone https://gitlab.com/aw5academy/terraform/clamav.git
As part of the Terraform stack we create an EC2 instance. This instance’s user data clones the repository at aw5academy/chef/clamav containing a Chef cookbook which, bootstraps the instance, installing ClamAV, mounting the EFS file system and configuring the virus database to point to a path on the EFS file system.
Lets now login to our EC2 instance to test our setup.