Saturday 28 June 2014

Installing the AWS .NET SDK on Mono/Linux using NuGet

Following a previous post about compiling the AWS .NET SDK on Mono/Linux +David Fevre asked whether it was possible to use NuGet to install the SDK. It turns out that this is actually quite a bit simpler if you are running Mono 3.2+. Below are the steps required to get the AWS SDK working on Mono/Linux (Ubuntu 14.04)

1. Install mono and associated tools, mozroots downloads root certificates to enable SSL via mono:
sudo apt-get update
sudo apt-get install mono-complete git wget
mozroots --import --sync


2. Create a working directory and change to it:
mkdir mono-aws
cd mono-aws

3. Download the command line version of NuGet and verify that it is version 2.8+:
wget http://nuget.org/nuget.exe
mono nuget.exe update -self


4. Install the AWSSDK via NuGet:
mono nuget.exe install AWSSDK

5. Create a symbolic link to the lib folder (note that the version number might be different from the example command):
ln -s AWSSDK.2.1.6.0/lib/net35 lib

6. Create a command line program to test with, for example in s3.cs:
using System;
using Amazon.S3;
using Amazon.S3.Model;

class Upload
{
  public static void Main(string[] args)
  {
    // Create a client
    AmazonS3Client client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);

    // Create a PutObject request
    PutObjectRequest request = new PutObjectRequest
    {
      BucketName = "SampleBucket",
      Key = "Item1",
      ContentBody = "This is sample content..."
    };

    // Put object
    client.PutObject(request);
    }
}


7. Compile your program:
mcs s3.cs -r:./lib/AWSSDK.dll

8. Set the Mono path
export MONO_PATH=`pwd`/lib:.

9. Create an application configuration (must be named to match the compiled program) for example in s3.exe.config:
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="AWSAccessKey" value="AKXXXXXXXXXXXXXX"/>
    <add key="AWSSecretKey" value="XXXXXXXXXXXXXXXXXXXXXXXX"/>
  </appSettings>
</configuration>


10. Run your program and verify that the object is created in the bucket:
mono s3.exe

Friday 27 June 2014

Code snippet: Adding a security group egress rule in boto

Quick example of adding an egress rule to an existing security group (turns out cidr_ip is actually required). Assumes you have boto installed and your AWS credentials configured:

import boto
c = boto.connect_ec2()
c.authorize_security_group_egress('sg-xxxxxxx', 'tcp', from_port=1024, to_port=1024, cidr_ip='0.0.0.0/0')