Cloning an AWS CloudFront Distribution Easily

I’m often creating a new site that’s similar to others and has CloudFront in front of them (yes, think WordPress). Many of these distributions have a bunch of behaviors, and I hate to waste time trying to recreate them (copying and pasting, making sure I didn’t miss a step). Anyway, finally taking a little bit of time, I’ve found it’s straightforward to do it using the AWS CLI:

Steps to export, modify, and import a new CF config file with AWS cli:

  1. Install the CLI if you haven’t already. It’s relatively quick and straightforward, provided you have your access and secret keys. I’ve been using Version 2 of the CLI with great success. I’m primarily on Windows, so the below steps are respective of that.
  2. Using the CLI, export the CloudFront config of a distribution you want to copy. In my below example, you’ll need to swap out the distribution ID with one of your real distribution IDs (something like E3xxxxxxxxxx). Also, the c:\aws folder is whatever you want.

    aws2 cloudfront get-distribution-config --id YourDistID --output json > c:\aws\sourceCFconfig.json
  3. Take this json file, sourceCFconfig.json, copy it to a new file, newCFconfig.json.
  4. Open the new file, edit out the first parent element (be sure to remove the last curly bracket at the end of the file as well).
  5. Edit the Aliases to reflect your new site info (or remove if you’re not doing HTTPS).
  6. If you’re using a different origin for this distribution, change that as well, including in the behaviors (make use of find and replace)!
  7. Near the end of the doc, change the comment to something new:
  8. Change the ViewerCertificate to a valid certificate ARN (or remove if you’re not using HTTPS).
  9. Save this file you’ve been editing.
  10. Import this config and create a new distribution using the below command:

    aws2 cloudfront create-distribution --distribution-config file://c:\aws\newCFconfig.json

If no errors are shown, you should see a new distribution in CloudFront.

Cloning an AWS CloudFront Distribution Easily

AWS RDS SQL Server – Using Memory Optimized Objects in your DB

If you’re using Amazon Web Services RDS for SQL Server (btw, it works great) and want to use Memory-Optimized Objects, it’s easy to alter your DB to allow this. Below is a little T-SQL. The key to this is the directory “D:\rdsdbdata\“, the default where RDS is storing your data.

-- Add new file group 
ALTER DATABASE [sampleDB] ADD FILEGROUP [MOD_FG] CONTAINS MEMORY_OPTIMIZED_DATA
GO
-- Add the new file
ALTER DATABASE [sampleDB] ADD FILE ( NAME = N'Mem_Opt_Data', FILENAME = N'D:\rdsdbdata\DATA\Mem_Opt_Data.ndf') TO FILEGROUP [MOD_FG]
GO
-- Set the MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT to On
ALTER DATABASE [sampleDB] SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = ON
GO

Things that you’ll need to swap out w/ your own info is “sampleDB” and “MOD_FG”.

Now your DB is all ready for some Memory-Optimized Objects.

Want to create a table that’s memory optimized, just use this:

-- Sample table create
CREATE TABLE testMOD (
SomeId INT IDENTITY(1,1)
,FirstName VARCHAR(30) NOT NULL
,LastName VARCHAR(30) NOT NULL
CONSTRAINT [PK_SomeID] PRIMARY KEY NONCLUSTERED HASH (SomeId) WITH (BUCKET_COUNT = 131072))
WITH(MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
GO

These Memory-Optimized Objects do offer a significant speed improvement but don’t go crazy w/ them.

AWS RDS SQL Server – Using Memory Optimized Objects in your DB

How To Create A Simple .Net Core (c#) AWS Lambda Function – Start to Finish

Creating a simple, serverless app w/ AWS Lambda is fairly easy, but some documentation out there is outdated or using the preview toolkit. Below are some steps that show how to do this today, in a few steps. I’m using Visual Studio 2017. I’m also going to assume you already have your AWS credentials on your machine, if not, that’s a different topic.

  1. Install the AWS Toolkit for Visual Studio 2017 for Visual Studio. This is required to give you the project templates.
  2. Open Visual Studio and start a new project, choose “AWS Lambda Project” and give your project a name (I picked “awsLambdaTest”)
  3. At the Blueprint choice, choose “empty” then click “finish”.
  4. Your project will now create after a few seconds and should look like this:
  5. You can edit your code (in Function.cs)
  6. If you don’t edit anything and publish
  7. Now give your function a name and choose “next”:
  8. Now choose a role, the lambda_exec role is fine, then click “upload”.
  9. By default, the Lambda Function view will appear – this allows you to test your function. If you enter a string in the box under “sample input” and press “invoke”, you’ll see your function response.
How To Create A Simple .Net Core (c#) AWS Lambda Function – Start to Finish

Creating Valid ZIP Archives of Multiple Files in C# / .Net

Are .zip files ever going away? I remember back in the 90’s using WinZip as an alternative to the PKZip command line option. Anyway, fast forward 20+ years, and ZIP is still common and a great way to package files. Long story short: AWS Elastic Beanstalk allows you to easily deploy apps using a .zip file (if you haven’t tried Elastic Beanstalk – it’s pretty awesome) and I wanted a faster way to create a .zip of an app. (Yes, I know it’s not the best way to deploy like Git or the API).

There is a bunch of great sample code out there for creating ZIP archives in c# .Net using the ZipArchive Class in System.IO.Compression, but nothing seems to be a complete sample, showing multiple files. Below is what I’ve been using. One difference in this is changing the path separators from backslashes to forward-slashes. Without this, AWS wasn’t able to extract my .zip archive. I would see errors such as: Continue reading “Creating Valid ZIP Archives of Multiple Files in C# / .Net”

Creating Valid ZIP Archives of Multiple Files in C# / .Net

Using Amazon Polly from .net / c#, Get MP3 File

knight-rider-car-kittIf you haven’t checked out Amazon’s new Polly (Text-to-Speech (TTS) cloud service) it does produce some pretty great, life-like audio. Below is a quick sample on how you can feed some text to Polly, and get an MP3 file with it. I don’t cover all of the install options of the AWS Toolkit for Visual Studio / .Net, but it’s pretty simple. (listen to this text here)

Below is some code:

AmazonPollyClient pc = new AmazonPollyClient();

SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
sreq.Text = "Your Sample Text Here";
sreq.OutputFormat = OutputFormat.Mp3;
sreq.VoiceId = VoiceId.Amy;
SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);

using (var fileStream = File.Create(@"c:\yourfile.mp3"))
{
sres.AudioStream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
}

 

Also make sure you have the below included:

using Amazon.Polly;
using Amazon.Polly.Model;

And these 2 NuGet packages added to your project:

aws-polly

This only scratches the surface of what Polly can do. Between streaming, SSML, Lexicons and more at a great price, I think we’ll be seeing more applications use this.

Using Amazon Polly from .net / c#, Get MP3 File